summaryrefslogtreecommitdiffstats
path: root/drivers/input/misc/cma3000_d0x_i2c.c
blob: a4dfb3052dc092722975757f169c7639082d77f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Implements I2C interface for VTI CMA300_D0x Accelerometer driver
 *
 * Copyright (C) 2010 Texas Instruments
 * Author: Hemanth V <hemanthv@ti.com>
 */

#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/input/cma3000.h>
#include "cma3000_d0x.h"

static int cma3000_i2c_set(struct device *dev,
			   u8 reg, u8 val, char *msg)
{
	struct i2c_client *client = to_i2c_client(dev);
	int ret;

	ret = i2c_smbus_write_byte_data(client, reg, val);
	if (ret < 0)
		dev_err(&client->dev,
			"%s failed (%s, %d)\n", __func__, msg, ret);
	return ret;
}

static int cma3000_i2c_read(struct device *dev, u8 reg, char *msg)
{
	struct i2c_client *client = to_i2c_client(dev);
	int ret;

	ret = i2c_smbus_read_byte_data(client, reg);
	if (ret < 0)
		dev_err(&client->dev,
			"%s failed (%s, %d)\n", __func__, msg, ret);
	return ret;
}

static const struct cma3000_bus_ops cma3000_i2c_bops = {
	.bustype	= BUS_I2C,
#define CMA3000_BUSI2C     (0 << 4)
	.ctrl_mod	= CMA3000_BUSI2C,
	.read		= cma3000_i2c_read,
	.write		= cma3000_i2c_set,
};

static int cma3000_i2c_probe(struct i2c_client *client)
{
	struct cma3000_accl_data *data;

	data = cma3000_init(&client->dev, client->irq, &cma3000_i2c_bops);
	if (IS_ERR(data))
		return PTR_ERR(data);

	i2c_set_clientdata(client, data);

	return 0;
}

static void cma3000_i2c_remove(struct i2c_client *client)
{
	struct cma3000_accl_data *data = i2c_get_clientdata(client);

	cma3000_exit(data);
}

static int cma3000_i2c_suspend(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct cma3000_accl_data *data = i2c_get_clientdata(client);

	cma3000_suspend(data);

	return 0;
}

static int cma3000_i2c_resume(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct cma3000_accl_data *data = i2c_get_clientdata(client);

	cma3000_resume(data);

	return 0;
}

static const struct dev_pm_ops cma3000_i2c_pm_ops = {
	.suspend	= cma3000_i2c_suspend,
	.resume		= cma3000_i2c_resume,
};

static const struct i2c_device_id cma3000_i2c_id[] = {
	{ "cma3000_d01", 0 },
	{ },
};

MODULE_DEVICE_TABLE(i2c, cma3000_i2c_id);

static struct i2c_driver cma3000_i2c_driver = {
	.probe		= cma3000_i2c_probe,
	.remove		= cma3000_i2c_remove,
	.id_table	= cma3000_i2c_id,
	.driver = {
		.name	= "cma3000_i2c_accl",
		.pm	= pm_sleep_ptr(&cma3000_i2c_pm_ops),
	},
};

module_i2c_driver(cma3000_i2c_driver);

MODULE_DESCRIPTION("CMA3000-D0x Accelerometer I2C Driver");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Hemanth V <hemanthv@ti.com>");