1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Implements I2C interface for VTI CMA300_D0x Accelerometer driver
4  *
5  * Copyright (C) 2010 Texas Instruments
6  * Author: Hemanth V <hemanthv@ti.com>
7  */
8 
9 #include <linux/module.h>
10 #include <linux/i2c.h>
11 #include <linux/input/cma3000.h>
12 #include "cma3000_d0x.h"
13 
14 static int cma3000_i2c_set(struct device *dev,
15 			   u8 reg, u8 val, char *msg)
16 {
17 	struct i2c_client *client = to_i2c_client(dev);
18 	int ret;
19 
20 	ret = i2c_smbus_write_byte_data(client, reg, val);
21 	if (ret < 0)
22 		dev_err(&client->dev,
23 			"%s failed (%s, %d)\n", __func__, msg, ret);
24 	return ret;
25 }
26 
27 static int cma3000_i2c_read(struct device *dev, u8 reg, char *msg)
28 {
29 	struct i2c_client *client = to_i2c_client(dev);
30 	int ret;
31 
32 	ret = i2c_smbus_read_byte_data(client, reg);
33 	if (ret < 0)
34 		dev_err(&client->dev,
35 			"%s failed (%s, %d)\n", __func__, msg, ret);
36 	return ret;
37 }
38 
39 static const struct cma3000_bus_ops cma3000_i2c_bops = {
40 	.bustype	= BUS_I2C,
41 #define CMA3000_BUSI2C     (0 << 4)
42 	.ctrl_mod	= CMA3000_BUSI2C,
43 	.read		= cma3000_i2c_read,
44 	.write		= cma3000_i2c_set,
45 };
46 
47 static int cma3000_i2c_probe(struct i2c_client *client,
48 					const struct i2c_device_id *id)
49 {
50 	struct cma3000_accl_data *data;
51 
52 	data = cma3000_init(&client->dev, client->irq, &cma3000_i2c_bops);
53 	if (IS_ERR(data))
54 		return PTR_ERR(data);
55 
56 	i2c_set_clientdata(client, data);
57 
58 	return 0;
59 }
60 
61 static int cma3000_i2c_remove(struct i2c_client *client)
62 {
63 	struct cma3000_accl_data *data = i2c_get_clientdata(client);
64 
65 	cma3000_exit(data);
66 
67 	return 0;
68 }
69 
70 #ifdef CONFIG_PM
71 static int cma3000_i2c_suspend(struct device *dev)
72 {
73 	struct i2c_client *client = to_i2c_client(dev);
74 	struct cma3000_accl_data *data = i2c_get_clientdata(client);
75 
76 	cma3000_suspend(data);
77 
78 	return 0;
79 }
80 
81 static int cma3000_i2c_resume(struct device *dev)
82 {
83 	struct i2c_client *client = to_i2c_client(dev);
84 	struct cma3000_accl_data *data = i2c_get_clientdata(client);
85 
86 	cma3000_resume(data);
87 
88 	return 0;
89 }
90 
91 static const struct dev_pm_ops cma3000_i2c_pm_ops = {
92 	.suspend	= cma3000_i2c_suspend,
93 	.resume		= cma3000_i2c_resume,
94 };
95 #endif
96 
97 static const struct i2c_device_id cma3000_i2c_id[] = {
98 	{ "cma3000_d01", 0 },
99 	{ },
100 };
101 
102 MODULE_DEVICE_TABLE(i2c, cma3000_i2c_id);
103 
104 static struct i2c_driver cma3000_i2c_driver = {
105 	.probe		= cma3000_i2c_probe,
106 	.remove		= cma3000_i2c_remove,
107 	.id_table	= cma3000_i2c_id,
108 	.driver = {
109 		.name	= "cma3000_i2c_accl",
110 #ifdef CONFIG_PM
111 		.pm	= &cma3000_i2c_pm_ops,
112 #endif
113 	},
114 };
115 
116 module_i2c_driver(cma3000_i2c_driver);
117 
118 MODULE_DESCRIPTION("CMA3000-D0x Accelerometer I2C Driver");
119 MODULE_LICENSE("GPL");
120 MODULE_AUTHOR("Hemanth V <hemanthv@ti.com>");
121