Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * ADLX345/346 Three-Axis Digital Accelerometers (I2C Interface)
4 *
5 * Enter bugs at http://blackfin.uclinux.org/
6 *
7 * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
8 */
9
10#include <linux/input.h> /* BUS_I2C */
11#include <linux/i2c.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/types.h>
15#include <linux/pm.h>
16#include "adxl34x.h"
17
18static int adxl34x_smbus_read(struct device *dev, unsigned char reg)
19{
20 struct i2c_client *client = to_i2c_client(dev);
21
22 return i2c_smbus_read_byte_data(client, reg);
23}
24
25static int adxl34x_smbus_write(struct device *dev,
26 unsigned char reg, unsigned char val)
27{
28 struct i2c_client *client = to_i2c_client(dev);
29
30 return i2c_smbus_write_byte_data(client, reg, val);
31}
32
33static int adxl34x_smbus_read_block(struct device *dev,
34 unsigned char reg, int count,
35 void *buf)
36{
37 struct i2c_client *client = to_i2c_client(dev);
38
39 return i2c_smbus_read_i2c_block_data(client, reg, count, buf);
40}
41
42static int adxl34x_i2c_read_block(struct device *dev,
43 unsigned char reg, int count,
44 void *buf)
45{
46 struct i2c_client *client = to_i2c_client(dev);
47 int ret;
48
49 ret = i2c_master_send(client, ®, 1);
50 if (ret < 0)
51 return ret;
52
53 ret = i2c_master_recv(client, buf, count);
54 if (ret < 0)
55 return ret;
56
57 if (ret != count)
58 return -EIO;
59
60 return 0;
61}
62
63static const struct adxl34x_bus_ops adxl34x_smbus_bops = {
64 .bustype = BUS_I2C,
65 .write = adxl34x_smbus_write,
66 .read = adxl34x_smbus_read,
67 .read_block = adxl34x_smbus_read_block,
68};
69
70static const struct adxl34x_bus_ops adxl34x_i2c_bops = {
71 .bustype = BUS_I2C,
72 .write = adxl34x_smbus_write,
73 .read = adxl34x_smbus_read,
74 .read_block = adxl34x_i2c_read_block,
75};
76
77static int adxl34x_i2c_probe(struct i2c_client *client)
78{
79 struct adxl34x *ac;
80 int error;
81
82 error = i2c_check_functionality(client->adapter,
83 I2C_FUNC_SMBUS_BYTE_DATA);
84 if (!error) {
85 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
86 return -EIO;
87 }
88
89 ac = adxl34x_probe(&client->dev, client->irq, false,
90 i2c_check_functionality(client->adapter,
91 I2C_FUNC_SMBUS_READ_I2C_BLOCK) ?
92 &adxl34x_smbus_bops : &adxl34x_i2c_bops);
93 if (IS_ERR(ac))
94 return PTR_ERR(ac);
95
96 i2c_set_clientdata(client, ac);
97
98 return 0;
99}
100
101static void adxl34x_i2c_remove(struct i2c_client *client)
102{
103 struct adxl34x *ac = i2c_get_clientdata(client);
104
105 adxl34x_remove(ac);
106}
107
108static int __maybe_unused adxl34x_i2c_suspend(struct device *dev)
109{
110 struct i2c_client *client = to_i2c_client(dev);
111 struct adxl34x *ac = i2c_get_clientdata(client);
112
113 adxl34x_suspend(ac);
114
115 return 0;
116}
117
118static int __maybe_unused adxl34x_i2c_resume(struct device *dev)
119{
120 struct i2c_client *client = to_i2c_client(dev);
121 struct adxl34x *ac = i2c_get_clientdata(client);
122
123 adxl34x_resume(ac);
124
125 return 0;
126}
127
128static SIMPLE_DEV_PM_OPS(adxl34x_i2c_pm, adxl34x_i2c_suspend,
129 adxl34x_i2c_resume);
130
131static const struct i2c_device_id adxl34x_id[] = {
132 { "adxl34x", 0 },
133 { }
134};
135
136MODULE_DEVICE_TABLE(i2c, adxl34x_id);
137
138static const struct of_device_id adxl34x_of_id[] = {
139 /*
140 * The ADXL346 is backward-compatible with the ADXL345. Differences are
141 * handled by runtime detection of the device model, there's thus no
142 * need for listing the "adi,adxl346" compatible value explicitly.
143 */
144 { .compatible = "adi,adxl345", },
145 /*
146 * Deprecated, DT nodes should use one or more of the device-specific
147 * compatible values "adi,adxl345" and "adi,adxl346".
148 */
149 { .compatible = "adi,adxl34x", },
150 { }
151};
152
153MODULE_DEVICE_TABLE(of, adxl34x_of_id);
154
155static struct i2c_driver adxl34x_driver = {
156 .driver = {
157 .name = "adxl34x",
158 .pm = &adxl34x_i2c_pm,
159 .of_match_table = adxl34x_of_id,
160 },
161 .probe_new = adxl34x_i2c_probe,
162 .remove = adxl34x_i2c_remove,
163 .id_table = adxl34x_id,
164};
165
166module_i2c_driver(adxl34x_driver);
167
168MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
169MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer I2C Bus Driver");
170MODULE_LICENSE("GPL");
1/*
2 * ADLX345/346 Three-Axis Digital Accelerometers (I2C Interface)
3 *
4 * Enter bugs at http://blackfin.uclinux.org/
5 *
6 * Copyright (C) 2009 Michael Hennerich, Analog Devices Inc.
7 * Licensed under the GPL-2 or later.
8 */
9
10#include <linux/input.h> /* BUS_I2C */
11#include <linux/i2c.h>
12#include <linux/module.h>
13#include <linux/types.h>
14#include <linux/pm.h>
15#include "adxl34x.h"
16
17static int adxl34x_smbus_read(struct device *dev, unsigned char reg)
18{
19 struct i2c_client *client = to_i2c_client(dev);
20
21 return i2c_smbus_read_byte_data(client, reg);
22}
23
24static int adxl34x_smbus_write(struct device *dev,
25 unsigned char reg, unsigned char val)
26{
27 struct i2c_client *client = to_i2c_client(dev);
28
29 return i2c_smbus_write_byte_data(client, reg, val);
30}
31
32static int adxl34x_smbus_read_block(struct device *dev,
33 unsigned char reg, int count,
34 void *buf)
35{
36 struct i2c_client *client = to_i2c_client(dev);
37
38 return i2c_smbus_read_i2c_block_data(client, reg, count, buf);
39}
40
41static int adxl34x_i2c_read_block(struct device *dev,
42 unsigned char reg, int count,
43 void *buf)
44{
45 struct i2c_client *client = to_i2c_client(dev);
46 int ret;
47
48 ret = i2c_master_send(client, ®, 1);
49 if (ret < 0)
50 return ret;
51
52 ret = i2c_master_recv(client, buf, count);
53 if (ret < 0)
54 return ret;
55
56 if (ret != count)
57 return -EIO;
58
59 return 0;
60}
61
62static const struct adxl34x_bus_ops adxl34x_smbus_bops = {
63 .bustype = BUS_I2C,
64 .write = adxl34x_smbus_write,
65 .read = adxl34x_smbus_read,
66 .read_block = adxl34x_smbus_read_block,
67};
68
69static const struct adxl34x_bus_ops adxl34x_i2c_bops = {
70 .bustype = BUS_I2C,
71 .write = adxl34x_smbus_write,
72 .read = adxl34x_smbus_read,
73 .read_block = adxl34x_i2c_read_block,
74};
75
76static int __devinit adxl34x_i2c_probe(struct i2c_client *client,
77 const struct i2c_device_id *id)
78{
79 struct adxl34x *ac;
80 int error;
81
82 error = i2c_check_functionality(client->adapter,
83 I2C_FUNC_SMBUS_BYTE_DATA);
84 if (!error) {
85 dev_err(&client->dev, "SMBUS Byte Data not Supported\n");
86 return -EIO;
87 }
88
89 ac = adxl34x_probe(&client->dev, client->irq, false,
90 i2c_check_functionality(client->adapter,
91 I2C_FUNC_SMBUS_READ_I2C_BLOCK) ?
92 &adxl34x_smbus_bops : &adxl34x_i2c_bops);
93 if (IS_ERR(ac))
94 return PTR_ERR(ac);
95
96 i2c_set_clientdata(client, ac);
97
98 return 0;
99}
100
101static int __devexit adxl34x_i2c_remove(struct i2c_client *client)
102{
103 struct adxl34x *ac = i2c_get_clientdata(client);
104
105 return adxl34x_remove(ac);
106}
107
108#ifdef CONFIG_PM
109static int adxl34x_i2c_suspend(struct device *dev)
110{
111 struct i2c_client *client = to_i2c_client(dev);
112 struct adxl34x *ac = i2c_get_clientdata(client);
113
114 adxl34x_suspend(ac);
115
116 return 0;
117}
118
119static int adxl34x_i2c_resume(struct device *dev)
120{
121 struct i2c_client *client = to_i2c_client(dev);
122 struct adxl34x *ac = i2c_get_clientdata(client);
123
124 adxl34x_resume(ac);
125
126 return 0;
127}
128#endif
129
130static SIMPLE_DEV_PM_OPS(adxl34x_i2c_pm, adxl34x_i2c_suspend,
131 adxl34x_i2c_resume);
132
133static const struct i2c_device_id adxl34x_id[] = {
134 { "adxl34x", 0 },
135 { }
136};
137
138MODULE_DEVICE_TABLE(i2c, adxl34x_id);
139
140static struct i2c_driver adxl34x_driver = {
141 .driver = {
142 .name = "adxl34x",
143 .owner = THIS_MODULE,
144 .pm = &adxl34x_i2c_pm,
145 },
146 .probe = adxl34x_i2c_probe,
147 .remove = __devexit_p(adxl34x_i2c_remove),
148 .id_table = adxl34x_id,
149};
150
151static int __init adxl34x_i2c_init(void)
152{
153 return i2c_add_driver(&adxl34x_driver);
154}
155module_init(adxl34x_i2c_init);
156
157static void __exit adxl34x_i2c_exit(void)
158{
159 i2c_del_driver(&adxl34x_driver);
160}
161module_exit(adxl34x_i2c_exit);
162
163MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
164MODULE_DESCRIPTION("ADXL345/346 Three-Axis Digital Accelerometer I2C Bus Driver");
165MODULE_LICENSE("GPL");