2 * 3-axis accelerometer driver for MXC4005XC Memsic sensor
4 * Copyright (c) 2014, Intel Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/iio/iio.h>
19 #include <linux/acpi.h>
20 #include <linux/regmap.h>
21 #include <linux/iio/sysfs.h>
23 #define MXC4005_DRV_NAME "mxc4005"
24 #define MXC4005_REGMAP_NAME "mxc4005_regmap"
26 #define MXC4005_REG_XOUT_UPPER 0x03
27 #define MXC4005_REG_XOUT_LOWER 0x04
28 #define MXC4005_REG_YOUT_UPPER 0x05
29 #define MXC4005_REG_YOUT_LOWER 0x06
30 #define MXC4005_REG_ZOUT_UPPER 0x07
31 #define MXC4005_REG_ZOUT_LOWER 0x08
33 #define MXC4005_REG_CONTROL 0x0D
34 #define MXC4005_REG_CONTROL_MASK_FSR GENMASK(6, 5)
35 #define MXC4005_CONTROL_FSR_SHIFT 5
37 #define MXC4005_REG_DEVICE_ID 0x0E
54 struct regmap *regmap;
58 * MXC4005 can operate in the following ranges:
59 * +/- 2G, 4G, 8G (the default +/-2G)
61 * (2 + 2) * 9.81 / (2^12 - 1) = 0.009582
62 * (4 + 4) * 9.81 / (2^12 - 1) = 0.019164
63 * (8 + 8) * 9.81 / (2^12 - 1) = 0.038329
68 } mxc4005_scale_table[] = {
69 {MXC4005_RANGE_2G, 9582},
70 {MXC4005_RANGE_4G, 19164},
71 {MXC4005_RANGE_8G, 38329},
75 static IIO_CONST_ATTR(in_accel_scale_available, "0.009582 0.019164 0.038329");
77 static struct attribute *mxc4005_attributes[] = {
78 &iio_const_attr_in_accel_scale_available.dev_attr.attr,
82 static const struct attribute_group mxc4005_attrs_group = {
83 .attrs = mxc4005_attributes,
86 static bool mxc4005_is_readable_reg(struct device *dev, unsigned int reg)
89 case MXC4005_REG_XOUT_UPPER:
90 case MXC4005_REG_XOUT_LOWER:
91 case MXC4005_REG_YOUT_UPPER:
92 case MXC4005_REG_YOUT_LOWER:
93 case MXC4005_REG_ZOUT_UPPER:
94 case MXC4005_REG_ZOUT_LOWER:
95 case MXC4005_REG_DEVICE_ID:
96 case MXC4005_REG_CONTROL:
103 static bool mxc4005_is_writeable_reg(struct device *dev, unsigned int reg)
106 case MXC4005_REG_CONTROL:
113 static const struct regmap_config mxc4005_regmap_config = {
114 .name = MXC4005_REGMAP_NAME,
119 .max_register = MXC4005_REG_DEVICE_ID,
121 .readable_reg = mxc4005_is_readable_reg,
122 .writeable_reg = mxc4005_is_writeable_reg,
125 static int mxc4005_read_axis(struct mxc4005_data *data,
131 ret = regmap_bulk_read(data->regmap, addr, (u8 *) ®, sizeof(reg));
133 dev_err(data->dev, "failed to read reg %02x\n", addr);
137 return be16_to_cpu(reg);
140 static int mxc4005_read_scale(struct mxc4005_data *data)
146 ret = regmap_read(data->regmap, MXC4005_REG_CONTROL, ®);
148 dev_err(data->dev, "failed to read reg_control\n");
152 i = reg >> MXC4005_CONTROL_FSR_SHIFT;
154 if (i < 0 || i >= ARRAY_SIZE(mxc4005_scale_table))
157 return mxc4005_scale_table[i].scale;
160 static int mxc4005_set_scale(struct mxc4005_data *data, int val)
166 for (i = 0; i < ARRAY_SIZE(mxc4005_scale_table); i++) {
167 if (mxc4005_scale_table[i].scale == val) {
168 reg = i << MXC4005_CONTROL_FSR_SHIFT;
169 ret = regmap_update_bits(data->regmap,
171 MXC4005_REG_CONTROL_MASK_FSR,
175 "failed to write reg_control\n");
183 static int mxc4005_read_raw(struct iio_dev *indio_dev,
184 struct iio_chan_spec const *chan,
185 int *val, int *val2, long mask)
187 struct mxc4005_data *data = iio_priv(indio_dev);
191 case IIO_CHAN_INFO_RAW:
192 switch (chan->type) {
194 if (iio_buffer_enabled(indio_dev))
197 ret = mxc4005_read_axis(data, chan->address);
200 *val = sign_extend32(ret >> 4, 11);
205 case IIO_CHAN_INFO_SCALE:
206 ret = mxc4005_read_scale(data);
212 return IIO_VAL_INT_PLUS_MICRO;
218 static int mxc4005_write_raw(struct iio_dev *indio_dev,
219 struct iio_chan_spec const *chan,
220 int val, int val2, long mask)
222 struct mxc4005_data *data = iio_priv(indio_dev);
225 case IIO_CHAN_INFO_SCALE:
229 return mxc4005_set_scale(data, val2);
235 static const struct iio_info mxc4005_info = {
236 .driver_module = THIS_MODULE,
237 .read_raw = mxc4005_read_raw,
238 .write_raw = mxc4005_write_raw,
239 .attrs = &mxc4005_attrs_group,
242 #define MXC4005_CHANNEL(_axis, _addr) { \
245 .channel2 = IIO_MOD_##_axis, \
247 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
248 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
251 static const struct iio_chan_spec mxc4005_channels[] = {
252 MXC4005_CHANNEL(X, MXC4005_REG_XOUT_UPPER),
253 MXC4005_CHANNEL(Y, MXC4005_REG_YOUT_UPPER),
254 MXC4005_CHANNEL(Z, MXC4005_REG_ZOUT_UPPER),
257 static int mxc4005_chip_init(struct mxc4005_data *data)
262 ret = regmap_read(data->regmap, MXC4005_REG_DEVICE_ID, ®);
264 dev_err(data->dev, "failed to read chip id\n");
268 dev_dbg(data->dev, "MXC4005 chip id %02x\n", reg);
273 static int mxc4005_probe(struct i2c_client *client,
274 const struct i2c_device_id *id)
276 struct mxc4005_data *data;
277 struct iio_dev *indio_dev;
278 struct regmap *regmap;
281 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
285 regmap = devm_regmap_init_i2c(client, &mxc4005_regmap_config);
286 if (IS_ERR(regmap)) {
287 dev_err(&client->dev, "failed to initialize regmap\n");
288 return PTR_ERR(regmap);
291 data = iio_priv(indio_dev);
292 i2c_set_clientdata(client, indio_dev);
293 data->dev = &client->dev;
294 data->regmap = regmap;
296 ret = mxc4005_chip_init(data);
298 dev_err(&client->dev, "failed to initialize chip\n");
302 mutex_init(&data->mutex);
304 indio_dev->dev.parent = &client->dev;
305 indio_dev->channels = mxc4005_channels;
306 indio_dev->num_channels = ARRAY_SIZE(mxc4005_channels);
307 indio_dev->name = MXC4005_DRV_NAME;
308 indio_dev->modes = INDIO_DIRECT_MODE;
309 indio_dev->info = &mxc4005_info;
311 ret = iio_device_register(indio_dev);
313 dev_err(&client->dev,
314 "unable to register iio device %d\n", ret);
321 static int mxc4005_remove(struct i2c_client *client)
323 iio_device_unregister(i2c_get_clientdata(client));
328 static const struct acpi_device_id mxc4005_acpi_match[] = {
332 MODULE_DEVICE_TABLE(acpi, mxc4005_acpi_match);
334 static const struct i2c_device_id mxc4005_id[] = {
338 MODULE_DEVICE_TABLE(i2c, mxc4005_id);
340 static struct i2c_driver mxc4005_driver = {
342 .name = MXC4005_DRV_NAME,
343 .acpi_match_table = ACPI_PTR(mxc4005_acpi_match),
345 .probe = mxc4005_probe,
346 .remove = mxc4005_remove,
347 .id_table = mxc4005_id,
350 module_i2c_driver(mxc4005_driver);
352 MODULE_AUTHOR("Teodora Baluta <teodora.baluta@intel.com>");
353 MODULE_LICENSE("GPL v2");
354 MODULE_DESCRIPTION("MXC4005 3-axis accelerometer driver");