1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * MXC6255 - MEMSIC orientation sensing accelerometer
4 *
5 * Copyright (c) 2015, Intel Corporation.
6 *
7 * IIO driver for MXC6255 (7-bit I2C slave address 0x15).
8 */
9
10 #include <linux/module.h>
11 #include <linux/i2c.h>
12 #include <linux/init.h>
13 #include <linux/iio/iio.h>
14 #include <linux/delay.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/regmap.h>
17 #include <linux/iio/sysfs.h>
18
19 #define MXC6255_DRV_NAME "mxc6255"
20
21 #define MXC6255_REG_XOUT 0x00
22 #define MXC6255_REG_YOUT 0x01
23 #define MXC6255_REG_CHIP_ID 0x08
24
25 #define MXC6255_CHIP_ID 0x05
26
27 /*
28 * MXC6255 has only one measurement range: +/- 2G.
29 * The acceleration output is an 8-bit value.
30 *
31 * Scale is calculated as follows:
32 * (2 + 2) * 9.80665 / (2^8 - 1) = 0.153829
33 *
34 * Scale value for +/- 2G measurement range
35 */
36 #define MXC6255_SCALE 153829
37
38 enum mxc6255_axis {
39 AXIS_X,
40 AXIS_Y,
41 };
42
43 struct mxc6255_data {
44 struct i2c_client *client;
45 struct regmap *regmap;
46 };
47
mxc6255_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)48 static int mxc6255_read_raw(struct iio_dev *indio_dev,
49 struct iio_chan_spec const *chan,
50 int *val, int *val2, long mask)
51 {
52 struct mxc6255_data *data = iio_priv(indio_dev);
53 unsigned int reg;
54 int ret;
55
56 switch (mask) {
57 case IIO_CHAN_INFO_RAW:
58 ret = regmap_read(data->regmap, chan->address, ®);
59 if (ret < 0) {
60 dev_err(&data->client->dev,
61 "Error reading reg %lu\n", chan->address);
62 return ret;
63 }
64
65 *val = sign_extend32(reg, 7);
66 return IIO_VAL_INT;
67 case IIO_CHAN_INFO_SCALE:
68 *val = 0;
69 *val2 = MXC6255_SCALE;
70 return IIO_VAL_INT_PLUS_MICRO;
71 default:
72 return -EINVAL;
73 }
74 }
75
76 static const struct iio_info mxc6255_info = {
77 .read_raw = mxc6255_read_raw,
78 };
79
80 #define MXC6255_CHANNEL(_axis, reg) { \
81 .type = IIO_ACCEL, \
82 .modified = 1, \
83 .channel2 = IIO_MOD_##_axis, \
84 .address = reg, \
85 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
86 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
87 }
88
89 static const struct iio_chan_spec mxc6255_channels[] = {
90 MXC6255_CHANNEL(X, MXC6255_REG_XOUT),
91 MXC6255_CHANNEL(Y, MXC6255_REG_YOUT),
92 };
93
mxc6255_is_readable_reg(struct device * dev,unsigned int reg)94 static bool mxc6255_is_readable_reg(struct device *dev, unsigned int reg)
95 {
96 switch (reg) {
97 case MXC6255_REG_XOUT:
98 case MXC6255_REG_YOUT:
99 case MXC6255_REG_CHIP_ID:
100 return true;
101 default:
102 return false;
103 }
104 }
105
106 static const struct regmap_config mxc6255_regmap_config = {
107 .name = "mxc6255_regmap",
108
109 .reg_bits = 8,
110 .val_bits = 8,
111
112 .readable_reg = mxc6255_is_readable_reg,
113 };
114
mxc6255_probe(struct i2c_client * client)115 static int mxc6255_probe(struct i2c_client *client)
116 {
117 struct mxc6255_data *data;
118 struct iio_dev *indio_dev;
119 struct regmap *regmap;
120 unsigned int chip_id;
121 int ret;
122
123 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
124 if (!indio_dev)
125 return -ENOMEM;
126
127 regmap = devm_regmap_init_i2c(client, &mxc6255_regmap_config);
128 if (IS_ERR(regmap)) {
129 dev_err(&client->dev, "Error initializing regmap\n");
130 return PTR_ERR(regmap);
131 }
132
133 data = iio_priv(indio_dev);
134 i2c_set_clientdata(client, indio_dev);
135 data->client = client;
136 data->regmap = regmap;
137
138 indio_dev->name = MXC6255_DRV_NAME;
139 indio_dev->channels = mxc6255_channels;
140 indio_dev->num_channels = ARRAY_SIZE(mxc6255_channels);
141 indio_dev->modes = INDIO_DIRECT_MODE;
142 indio_dev->info = &mxc6255_info;
143
144 ret = regmap_read(data->regmap, MXC6255_REG_CHIP_ID, &chip_id);
145 if (ret < 0) {
146 dev_err(&client->dev, "Error reading chip id %d\n", ret);
147 return ret;
148 }
149
150 if ((chip_id & 0x1f) != MXC6255_CHIP_ID) {
151 dev_err(&client->dev, "Invalid chip id %x\n", chip_id);
152 return -ENODEV;
153 }
154
155 dev_dbg(&client->dev, "Chip id %x\n", chip_id);
156
157 ret = devm_iio_device_register(&client->dev, indio_dev);
158 if (ret < 0) {
159 dev_err(&client->dev, "Could not register IIO device\n");
160 return ret;
161 }
162
163 return 0;
164 }
165
166 static const struct acpi_device_id mxc6255_acpi_match[] = {
167 {"MXC6225", 0},
168 {"MXC6255", 0},
169 { }
170 };
171 MODULE_DEVICE_TABLE(acpi, mxc6255_acpi_match);
172
173 static const struct i2c_device_id mxc6255_id[] = {
174 { "mxc6225" },
175 { "mxc6255" },
176 { }
177 };
178 MODULE_DEVICE_TABLE(i2c, mxc6255_id);
179
180 static struct i2c_driver mxc6255_driver = {
181 .driver = {
182 .name = MXC6255_DRV_NAME,
183 .acpi_match_table = mxc6255_acpi_match,
184 },
185 .probe = mxc6255_probe,
186 .id_table = mxc6255_id,
187 };
188
189 module_i2c_driver(mxc6255_driver);
190
191 MODULE_AUTHOR("Teodora Baluta <teodora.baluta@intel.com>");
192 MODULE_DESCRIPTION("MEMSIC MXC6255 orientation sensing accelerometer driver");
193 MODULE_LICENSE("GPL v2");
194