Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * mCube MC3230 3-Axis Accelerometer
4 *
5 * Copyright (c) 2016 Hans de Goede <hdegoede@redhat.com>
6 *
7 * IIO driver for mCube MC3230; 7-bit I2C address: 0x4c.
8 */
9
10#include <linux/i2c.h>
11#include <linux/module.h>
12#include <linux/iio/iio.h>
13#include <linux/iio/sysfs.h>
14
15#define MC3230_REG_XOUT 0x00
16#define MC3230_REG_YOUT 0x01
17#define MC3230_REG_ZOUT 0x02
18
19#define MC3230_REG_MODE 0x07
20#define MC3230_MODE_OPCON_MASK 0x03
21#define MC3230_MODE_OPCON_WAKE 0x01
22#define MC3230_MODE_OPCON_STANDBY 0x03
23
24#define MC3230_REG_CHIP_ID 0x18
25#define MC3230_REG_PRODUCT_CODE 0x3b
26
27/*
28 * The accelerometer has one measurement range:
29 *
30 * -1.5g - +1.5g (8-bit, signed)
31 *
32 */
33
34struct mc3230_chip_info {
35 const char *name;
36 const u8 chip_id;
37 const u8 product_code;
38 const int scale;
39};
40
41static const struct mc3230_chip_info mc3230_chip_info = {
42 .name = "mc3230",
43 .chip_id = 0x01,
44 .product_code = 0x19,
45 /* (1.5 + 1.5) * 9.81 / (2^8 - 1) = 0.115411765 */
46 .scale = 115411765,
47};
48
49static const struct mc3230_chip_info mc3510c_chip_info = {
50 .name = "mc3510c",
51 .chip_id = 0x23,
52 .product_code = 0x10,
53 /* Was obtained empirically */
54 .scale = 625000000,
55};
56
57#define MC3230_CHANNEL(reg, axis) { \
58 .type = IIO_ACCEL, \
59 .address = reg, \
60 .modified = 1, \
61 .channel2 = IIO_MOD_##axis, \
62 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
63 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
64 .ext_info = mc3230_ext_info, \
65}
66
67struct mc3230_data {
68 const struct mc3230_chip_info *chip_info;
69 struct i2c_client *client;
70 struct iio_mount_matrix orientation;
71};
72
73static const struct iio_mount_matrix *
74mc3230_get_mount_matrix(const struct iio_dev *indio_dev,
75 const struct iio_chan_spec *chan)
76{
77 struct mc3230_data *data = iio_priv(indio_dev);
78
79 return &data->orientation;
80}
81
82static const struct iio_chan_spec_ext_info mc3230_ext_info[] = {
83 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, mc3230_get_mount_matrix),
84 { }
85};
86
87static const struct iio_chan_spec mc3230_channels[] = {
88 MC3230_CHANNEL(MC3230_REG_XOUT, X),
89 MC3230_CHANNEL(MC3230_REG_YOUT, Y),
90 MC3230_CHANNEL(MC3230_REG_ZOUT, Z),
91};
92
93static int mc3230_set_opcon(struct mc3230_data *data, int opcon)
94{
95 int ret;
96 struct i2c_client *client = data->client;
97
98 ret = i2c_smbus_read_byte_data(client, MC3230_REG_MODE);
99 if (ret < 0) {
100 dev_err(&client->dev, "failed to read mode reg: %d\n", ret);
101 return ret;
102 }
103
104 ret &= ~MC3230_MODE_OPCON_MASK;
105 ret |= opcon;
106
107 ret = i2c_smbus_write_byte_data(client, MC3230_REG_MODE, ret);
108 if (ret < 0) {
109 dev_err(&client->dev, "failed to write mode reg: %d\n", ret);
110 return ret;
111 }
112
113 return 0;
114}
115
116static int mc3230_read_raw(struct iio_dev *indio_dev,
117 struct iio_chan_spec const *chan,
118 int *val, int *val2, long mask)
119{
120 struct mc3230_data *data = iio_priv(indio_dev);
121 int ret;
122
123 switch (mask) {
124 case IIO_CHAN_INFO_RAW:
125 ret = i2c_smbus_read_byte_data(data->client, chan->address);
126 if (ret < 0)
127 return ret;
128 *val = sign_extend32(ret, 7);
129 return IIO_VAL_INT;
130 case IIO_CHAN_INFO_SCALE:
131 *val = 0;
132 *val2 = data->chip_info->scale;
133 return IIO_VAL_INT_PLUS_NANO;
134 default:
135 return -EINVAL;
136 }
137}
138
139static const struct iio_info mc3230_info = {
140 .read_raw = mc3230_read_raw,
141};
142
143static int mc3230_probe(struct i2c_client *client)
144{
145 int ret;
146 struct iio_dev *indio_dev;
147 struct mc3230_data *data;
148 const struct mc3230_chip_info *chip_info;
149
150 chip_info = i2c_get_match_data(client);
151 if (chip_info == NULL) {
152 dev_err(&client->dev, "failed to get match data");
153 return -ENODATA;
154 }
155
156 /* First check chip-id and product-id */
157 ret = i2c_smbus_read_byte_data(client, MC3230_REG_CHIP_ID);
158 if (ret != chip_info->chip_id) {
159 dev_info(&client->dev,
160 "chip id check fail: 0x%x != 0x%x !\n",
161 ret, chip_info->chip_id);
162 }
163
164 ret = i2c_smbus_read_byte_data(client, MC3230_REG_PRODUCT_CODE);
165 if (ret != chip_info->product_code) {
166 dev_info(&client->dev,
167 "product code check fail: 0x%x != 0x%x !\n",
168 ret, chip_info->product_code);
169 }
170
171 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
172 if (!indio_dev)
173 return -ENOMEM;
174
175 data = iio_priv(indio_dev);
176 data->chip_info = chip_info;
177 data->client = client;
178 i2c_set_clientdata(client, indio_dev);
179
180 indio_dev->info = &mc3230_info;
181 indio_dev->name = chip_info->name;
182 indio_dev->modes = INDIO_DIRECT_MODE;
183 indio_dev->channels = mc3230_channels;
184 indio_dev->num_channels = ARRAY_SIZE(mc3230_channels);
185
186 ret = mc3230_set_opcon(data, MC3230_MODE_OPCON_WAKE);
187 if (ret < 0)
188 return ret;
189
190 ret = iio_read_mount_matrix(&client->dev, &data->orientation);
191 if (ret)
192 return ret;
193
194 ret = iio_device_register(indio_dev);
195 if (ret < 0) {
196 dev_err(&client->dev, "device_register failed\n");
197 mc3230_set_opcon(data, MC3230_MODE_OPCON_STANDBY);
198 }
199
200 return ret;
201}
202
203static void mc3230_remove(struct i2c_client *client)
204{
205 struct iio_dev *indio_dev = i2c_get_clientdata(client);
206
207 iio_device_unregister(indio_dev);
208
209 mc3230_set_opcon(iio_priv(indio_dev), MC3230_MODE_OPCON_STANDBY);
210}
211
212static int mc3230_suspend(struct device *dev)
213{
214 struct mc3230_data *data;
215
216 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
217
218 return mc3230_set_opcon(data, MC3230_MODE_OPCON_STANDBY);
219}
220
221static int mc3230_resume(struct device *dev)
222{
223 struct mc3230_data *data;
224
225 data = iio_priv(i2c_get_clientdata(to_i2c_client(dev)));
226
227 return mc3230_set_opcon(data, MC3230_MODE_OPCON_WAKE);
228}
229
230static DEFINE_SIMPLE_DEV_PM_OPS(mc3230_pm_ops, mc3230_suspend, mc3230_resume);
231
232static const struct i2c_device_id mc3230_i2c_id[] = {
233 { "mc3230", (kernel_ulong_t)&mc3230_chip_info },
234 { "mc3510c", (kernel_ulong_t)&mc3510c_chip_info },
235 { }
236};
237MODULE_DEVICE_TABLE(i2c, mc3230_i2c_id);
238
239static const struct of_device_id mc3230_of_match[] = {
240 { .compatible = "mcube,mc3230", &mc3230_chip_info },
241 { .compatible = "mcube,mc3510c", &mc3510c_chip_info },
242 { }
243};
244MODULE_DEVICE_TABLE(of, mc3230_of_match);
245
246static struct i2c_driver mc3230_driver = {
247 .driver = {
248 .name = "mc3230",
249 .of_match_table = mc3230_of_match,
250 .pm = pm_sleep_ptr(&mc3230_pm_ops),
251 },
252 .probe = mc3230_probe,
253 .remove = mc3230_remove,
254 .id_table = mc3230_i2c_id,
255};
256
257module_i2c_driver(mc3230_driver);
258
259MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
260MODULE_DESCRIPTION("mCube MC3230 3-Axis Accelerometer driver");
261MODULE_LICENSE("GPL v2");