]>
Commit | Line | Data |
---|---|---|
abeb6b1e DB |
1 | /* |
2 | * MMC35240 - MEMSIC 3-axis Magnetic Sensor | |
3 | * | |
4 | * Copyright (c) 2015, Intel Corporation. | |
5 | * | |
6 | * This file is subject to the terms and conditions of version 2 of | |
7 | * the GNU General Public License. See the file COPYING in the main | |
8 | * directory of this archive for more details. | |
9 | * | |
10 | * IIO driver for MMC35240 (7-bit I2C slave address 0x30). | |
11 | * | |
12 | * TODO: offset, ACPI, continuous measurement mode, PM | |
13 | */ | |
14 | ||
15 | #include <linux/module.h> | |
16 | #include <linux/init.h> | |
17 | #include <linux/i2c.h> | |
18 | #include <linux/delay.h> | |
19 | #include <linux/regmap.h> | |
20 | ||
21 | #include <linux/iio/iio.h> | |
22 | #include <linux/iio/sysfs.h> | |
23 | ||
24 | #define MMC35240_DRV_NAME "mmc35240" | |
25 | #define MMC35240_REGMAP_NAME "mmc35240_regmap" | |
26 | ||
27 | #define MMC35240_REG_XOUT_L 0x00 | |
28 | #define MMC35240_REG_XOUT_H 0x01 | |
29 | #define MMC35240_REG_YOUT_L 0x02 | |
30 | #define MMC35240_REG_YOUT_H 0x03 | |
31 | #define MMC35240_REG_ZOUT_L 0x04 | |
32 | #define MMC35240_REG_ZOUT_H 0x05 | |
33 | ||
34 | #define MMC35240_REG_STATUS 0x06 | |
35 | #define MMC35240_REG_CTRL0 0x07 | |
36 | #define MMC35240_REG_CTRL1 0x08 | |
37 | ||
38 | #define MMC35240_REG_ID 0x20 | |
39 | ||
40 | #define MMC35240_STATUS_MEAS_DONE_BIT BIT(0) | |
41 | ||
42 | #define MMC35240_CTRL0_REFILL_BIT BIT(7) | |
43 | #define MMC35240_CTRL0_RESET_BIT BIT(6) | |
44 | #define MMC35240_CTRL0_SET_BIT BIT(5) | |
45 | #define MMC35240_CTRL0_CMM_BIT BIT(1) | |
46 | #define MMC35240_CTRL0_TM_BIT BIT(0) | |
47 | ||
48 | /* output resolution bits */ | |
49 | #define MMC35240_CTRL1_BW0_BIT BIT(0) | |
50 | #define MMC35240_CTRL1_BW1_BIT BIT(1) | |
51 | ||
52 | #define MMC35240_CTRL1_BW_MASK (MMC35240_CTRL1_BW0_BIT | \ | |
53 | MMC35240_CTRL1_BW1_BIT) | |
54 | #define MMC35240_CTRL1_BW_SHIFT 0 | |
55 | ||
56 | #define MMC35240_WAIT_CHARGE_PUMP 50000 /* us */ | |
57 | #define MMC53240_WAIT_SET_RESET 1000 /* us */ | |
58 | ||
59 | enum mmc35240_resolution { | |
60 | MMC35240_16_BITS_SLOW = 0, /* 100 Hz */ | |
61 | MMC35240_16_BITS_FAST, /* 200 Hz */ | |
62 | MMC35240_14_BITS, /* 333 Hz */ | |
63 | MMC35240_12_BITS, /* 666 Hz */ | |
64 | }; | |
65 | ||
66 | enum mmc35240_axis { | |
67 | AXIS_X = 0, | |
68 | AXIS_Y, | |
69 | AXIS_Z, | |
70 | }; | |
71 | ||
72 | static const struct { | |
73 | int sens[3]; /* sensitivity per X, Y, Z axis */ | |
74 | int nfo; /* null field output */ | |
75 | } mmc35240_props_table[] = { | |
76 | /* 16 bits, 100Hz ODR */ | |
77 | { | |
78 | {1024, 1024, 770}, | |
79 | 32768, | |
80 | }, | |
81 | /* 16 bits, 200Hz ODR */ | |
82 | { | |
83 | {1024, 1024, 770}, | |
84 | 32768, | |
85 | }, | |
86 | /* 14 bits, 333Hz ODR */ | |
87 | { | |
88 | {256, 256, 193}, | |
89 | 8192, | |
90 | }, | |
91 | /* 12 bits, 666Hz ODR */ | |
92 | { | |
93 | {64, 64, 48}, | |
94 | 2048, | |
95 | }, | |
96 | }; | |
97 | ||
98 | struct mmc35240_data { | |
99 | struct i2c_client *client; | |
100 | struct mutex mutex; | |
101 | struct regmap *regmap; | |
102 | enum mmc35240_resolution res; | |
103 | }; | |
104 | ||
105 | static const int mmc35240_samp_freq[] = {100, 200, 333, 666}; | |
106 | ||
107 | static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("100 200 333 666"); | |
108 | ||
109 | #define MMC35240_CHANNEL(_axis) { \ | |
110 | .type = IIO_MAGN, \ | |
111 | .modified = 1, \ | |
112 | .channel2 = IIO_MOD_ ## _axis, \ | |
113 | .address = AXIS_ ## _axis, \ | |
114 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \ | |
115 | .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ), \ | |
116 | } | |
117 | ||
118 | static const struct iio_chan_spec mmc35240_channels[] = { | |
119 | MMC35240_CHANNEL(X), | |
120 | MMC35240_CHANNEL(Y), | |
121 | MMC35240_CHANNEL(Z), | |
122 | }; | |
123 | ||
124 | static struct attribute *mmc35240_attributes[] = { | |
125 | &iio_const_attr_sampling_frequency_available.dev_attr.attr, | |
126 | }; | |
127 | ||
128 | static const struct attribute_group mmc35240_attribute_group = { | |
129 | .attrs = mmc35240_attributes, | |
130 | }; | |
131 | ||
132 | static int mmc35240_get_samp_freq_index(struct mmc35240_data *data, | |
133 | int val, int val2) | |
134 | { | |
135 | int i; | |
136 | ||
137 | for (i = 0; i < ARRAY_SIZE(mmc35240_samp_freq); i++) | |
138 | if (mmc35240_samp_freq[i] == val) | |
139 | return i; | |
140 | return -EINVAL; | |
141 | } | |
142 | ||
143 | static int mmc35240_hw_set(struct mmc35240_data *data, bool set) | |
144 | { | |
145 | int ret; | |
146 | u8 coil_bit; | |
147 | ||
148 | /* | |
149 | * Recharge the capacitor at VCAP pin, requested to be issued | |
150 | * before a SET/RESET command. | |
151 | */ | |
152 | ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL0, | |
153 | MMC35240_CTRL0_REFILL_BIT, | |
154 | MMC35240_CTRL0_REFILL_BIT); | |
155 | if (ret < 0) | |
156 | return ret; | |
157 | usleep_range(MMC35240_WAIT_CHARGE_PUMP, MMC35240_WAIT_CHARGE_PUMP + 1); | |
158 | ||
159 | if (set) | |
160 | coil_bit = MMC35240_CTRL0_SET_BIT; | |
161 | else | |
162 | coil_bit = MMC35240_CTRL0_RESET_BIT; | |
163 | ||
164 | return regmap_update_bits(data->regmap, MMC35240_REG_CTRL0, | |
165 | MMC35240_CTRL0_REFILL_BIT, | |
166 | coil_bit); | |
167 | } | |
168 | ||
169 | static int mmc35240_init(struct mmc35240_data *data) | |
170 | { | |
171 | int ret; | |
172 | unsigned int reg_id; | |
173 | ||
174 | ret = regmap_read(data->regmap, MMC35240_REG_ID, ®_id); | |
175 | if (ret < 0) { | |
176 | dev_err(&data->client->dev, "Error reading product id\n"); | |
177 | return ret; | |
178 | } | |
179 | ||
180 | dev_dbg(&data->client->dev, "MMC35240 chip id %x\n", reg_id); | |
181 | ||
182 | /* | |
183 | * make sure we restore sensor characteristics, by doing | |
184 | * a RESET/SET sequence | |
185 | */ | |
186 | ret = mmc35240_hw_set(data, false); | |
187 | if (ret < 0) | |
188 | return ret; | |
189 | usleep_range(MMC53240_WAIT_SET_RESET, MMC53240_WAIT_SET_RESET + 1); | |
190 | ||
191 | ret = mmc35240_hw_set(data, true); | |
192 | if (ret < 0) | |
193 | return ret; | |
194 | ||
195 | /* set default sampling frequency */ | |
196 | return regmap_update_bits(data->regmap, MMC35240_REG_CTRL1, | |
197 | MMC35240_CTRL1_BW_MASK, | |
198 | data->res << MMC35240_CTRL1_BW_SHIFT); | |
199 | } | |
200 | ||
201 | static int mmc35240_take_measurement(struct mmc35240_data *data) | |
202 | { | |
203 | int ret, tries = 100; | |
204 | unsigned int reg_status; | |
205 | ||
206 | ret = regmap_write(data->regmap, MMC35240_REG_CTRL0, | |
207 | MMC35240_CTRL0_TM_BIT); | |
208 | if (ret < 0) | |
209 | return ret; | |
210 | ||
211 | while (tries-- > 0) { | |
212 | ret = regmap_read(data->regmap, MMC35240_REG_STATUS, | |
213 | ®_status); | |
214 | if (ret < 0) | |
215 | return ret; | |
216 | if (reg_status & MMC35240_STATUS_MEAS_DONE_BIT) | |
217 | break; | |
218 | msleep(20); | |
219 | } | |
220 | ||
221 | if (tries < 0) { | |
222 | dev_err(&data->client->dev, "data not ready\n"); | |
223 | return -EIO; | |
224 | } | |
225 | ||
226 | return 0; | |
227 | } | |
228 | ||
229 | static int mmc35240_read_measurement(struct mmc35240_data *data, __le16 buf[3]) | |
230 | { | |
231 | int ret; | |
232 | ||
233 | ret = mmc35240_take_measurement(data); | |
234 | if (ret < 0) | |
235 | return ret; | |
236 | ||
237 | return regmap_bulk_read(data->regmap, MMC35240_REG_XOUT_L, (u8 *)buf, | |
238 | 3 * sizeof(__le16)); | |
239 | } | |
240 | ||
241 | static int mmc35240_raw_to_gauss(struct mmc35240_data *data, int index, | |
242 | __le16 buf[], | |
243 | int *val, int *val2) | |
244 | { | |
245 | int raw_x, raw_y, raw_z; | |
246 | int sens_x, sens_y, sens_z; | |
247 | int nfo; | |
248 | ||
249 | raw_x = le16_to_cpu(buf[AXIS_X]); | |
250 | raw_y = le16_to_cpu(buf[AXIS_Y]); | |
251 | raw_z = le16_to_cpu(buf[AXIS_Z]); | |
252 | ||
253 | sens_x = mmc35240_props_table[data->res].sens[AXIS_X]; | |
254 | sens_y = mmc35240_props_table[data->res].sens[AXIS_Y]; | |
255 | sens_z = mmc35240_props_table[data->res].sens[AXIS_Z]; | |
256 | ||
257 | nfo = mmc35240_props_table[data->res].nfo; | |
258 | ||
259 | switch (index) { | |
260 | case AXIS_X: | |
261 | *val = (raw_x - nfo) / sens_x; | |
262 | *val2 = ((raw_x - nfo) % sens_x) * 1000000; | |
263 | break; | |
264 | case AXIS_Y: | |
265 | *val = (raw_y - nfo) / sens_y - (raw_z - nfo) / sens_z; | |
266 | *val2 = (((raw_y - nfo) % sens_y - (raw_z - nfo) % sens_z)) | |
267 | * 1000000; | |
268 | break; | |
269 | case AXIS_Z: | |
270 | *val = (raw_y - nfo) / sens_y + (raw_z - nfo) / sens_z; | |
271 | *val2 = (((raw_y - nfo) % sens_y + (raw_z - nfo) % sens_z)) | |
272 | * 1000000; | |
273 | break; | |
274 | default: | |
275 | return -EINVAL; | |
276 | } | |
277 | return 0; | |
278 | } | |
279 | ||
280 | static int mmc35240_read_raw(struct iio_dev *indio_dev, | |
281 | struct iio_chan_spec const *chan, int *val, | |
282 | int *val2, long mask) | |
283 | { | |
284 | struct mmc35240_data *data = iio_priv(indio_dev); | |
285 | int ret, i; | |
286 | unsigned int reg; | |
287 | __le16 buf[3]; | |
288 | ||
289 | switch (mask) { | |
290 | case IIO_CHAN_INFO_PROCESSED: | |
291 | mutex_lock(&data->mutex); | |
292 | ret = mmc35240_read_measurement(data, buf); | |
293 | mutex_unlock(&data->mutex); | |
294 | if (ret < 0) | |
295 | return ret; | |
296 | ret = mmc35240_raw_to_gauss(data, chan->address, | |
297 | buf, val, val2); | |
298 | if (ret < 0) | |
299 | return ret; | |
300 | return IIO_VAL_INT_PLUS_MICRO; | |
301 | case IIO_CHAN_INFO_SAMP_FREQ: | |
302 | mutex_lock(&data->mutex); | |
303 | ret = regmap_read(data->regmap, MMC35240_REG_CTRL1, ®); | |
304 | mutex_unlock(&data->mutex); | |
305 | if (ret < 0) | |
306 | return ret; | |
307 | ||
308 | i = (reg & MMC35240_CTRL1_BW_MASK) >> MMC35240_CTRL1_BW_SHIFT; | |
309 | if (i < 0 || i > ARRAY_SIZE(mmc35240_samp_freq)) | |
310 | return -EINVAL; | |
311 | ||
312 | *val = mmc35240_samp_freq[i]; | |
313 | *val2 = 0; | |
314 | return IIO_VAL_INT; | |
315 | default: | |
316 | return -EINVAL; | |
317 | } | |
318 | } | |
319 | ||
320 | static int mmc35240_write_raw(struct iio_dev *indio_dev, | |
321 | struct iio_chan_spec const *chan, int val, | |
322 | int val2, long mask) | |
323 | { | |
324 | struct mmc35240_data *data = iio_priv(indio_dev); | |
325 | int i, ret; | |
326 | ||
327 | switch (mask) { | |
328 | case IIO_CHAN_INFO_SAMP_FREQ: | |
329 | i = mmc35240_get_samp_freq_index(data, val, val2); | |
330 | if (i < 0) | |
331 | return -EINVAL; | |
332 | mutex_lock(&data->mutex); | |
333 | ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL1, | |
334 | MMC35240_CTRL1_BW_MASK, | |
335 | i << MMC35240_CTRL1_BW_SHIFT); | |
336 | mutex_unlock(&data->mutex); | |
337 | return ret; | |
338 | default: | |
339 | return -EINVAL; | |
340 | } | |
341 | } | |
342 | ||
343 | static const struct iio_info mmc35240_info = { | |
344 | .driver_module = THIS_MODULE, | |
345 | .read_raw = mmc35240_read_raw, | |
346 | .write_raw = mmc35240_write_raw, | |
347 | .attrs = &mmc35240_attribute_group, | |
348 | }; | |
349 | ||
350 | static bool mmc35240_is_writeable_reg(struct device *dev, unsigned int reg) | |
351 | { | |
352 | switch (reg) { | |
353 | case MMC35240_REG_CTRL0: | |
354 | case MMC35240_REG_CTRL1: | |
355 | return true; | |
356 | default: | |
357 | return false; | |
358 | } | |
359 | } | |
360 | ||
361 | static bool mmc35240_is_readable_reg(struct device *dev, unsigned int reg) | |
362 | { | |
363 | switch (reg) { | |
364 | case MMC35240_REG_XOUT_L: | |
365 | case MMC35240_REG_XOUT_H: | |
366 | case MMC35240_REG_YOUT_L: | |
367 | case MMC35240_REG_YOUT_H: | |
368 | case MMC35240_REG_ZOUT_L: | |
369 | case MMC35240_REG_ZOUT_H: | |
370 | case MMC35240_REG_STATUS: | |
371 | case MMC35240_REG_ID: | |
372 | return true; | |
373 | default: | |
374 | return false; | |
375 | } | |
376 | } | |
377 | ||
378 | static bool mmc35240_is_volatile_reg(struct device *dev, unsigned int reg) | |
379 | { | |
380 | switch (reg) { | |
381 | case MMC35240_REG_CTRL0: | |
382 | case MMC35240_REG_CTRL1: | |
383 | return false; | |
384 | default: | |
385 | return true; | |
386 | } | |
387 | } | |
388 | ||
389 | static struct reg_default mmc35240_reg_defaults[] = { | |
390 | { MMC35240_REG_CTRL0, 0x00 }, | |
391 | { MMC35240_REG_CTRL1, 0x00 }, | |
392 | }; | |
393 | ||
394 | static const struct regmap_config mmc35240_regmap_config = { | |
395 | .name = MMC35240_REGMAP_NAME, | |
396 | ||
397 | .reg_bits = 8, | |
398 | .val_bits = 8, | |
399 | ||
400 | .max_register = MMC35240_REG_ID, | |
401 | .cache_type = REGCACHE_FLAT, | |
402 | ||
403 | .writeable_reg = mmc35240_is_writeable_reg, | |
404 | .readable_reg = mmc35240_is_readable_reg, | |
405 | .volatile_reg = mmc35240_is_volatile_reg, | |
406 | ||
407 | .reg_defaults = mmc35240_reg_defaults, | |
408 | .num_reg_defaults = ARRAY_SIZE(mmc35240_reg_defaults), | |
409 | }; | |
410 | ||
411 | static int mmc35240_probe(struct i2c_client *client, | |
412 | const struct i2c_device_id *id) | |
413 | { | |
414 | struct mmc35240_data *data; | |
415 | struct iio_dev *indio_dev; | |
416 | struct regmap *regmap; | |
417 | int ret; | |
418 | ||
419 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); | |
420 | if (!indio_dev) | |
421 | return -ENOMEM; | |
422 | ||
423 | regmap = devm_regmap_init_i2c(client, &mmc35240_regmap_config); | |
424 | if (IS_ERR(regmap)) { | |
425 | dev_err(&client->dev, "regmap initialization failed\n"); | |
426 | return PTR_ERR(regmap); | |
427 | } | |
428 | ||
429 | data = iio_priv(indio_dev); | |
430 | data->client = client; | |
431 | data->regmap = regmap; | |
432 | data->res = MMC35240_16_BITS_SLOW; | |
433 | ||
434 | mutex_init(&data->mutex); | |
435 | ||
436 | indio_dev->dev.parent = &client->dev; | |
437 | indio_dev->info = &mmc35240_info; | |
438 | indio_dev->name = MMC35240_DRV_NAME; | |
439 | indio_dev->channels = mmc35240_channels; | |
440 | indio_dev->num_channels = ARRAY_SIZE(mmc35240_channels); | |
441 | indio_dev->modes = INDIO_DIRECT_MODE; | |
442 | ||
443 | ret = mmc35240_init(data); | |
444 | if (ret < 0) { | |
445 | dev_err(&client->dev, "mmc35240 chip init failed\n"); | |
446 | return ret; | |
447 | } | |
448 | return devm_iio_device_register(&client->dev, indio_dev); | |
449 | } | |
450 | ||
451 | static const struct i2c_device_id mmc35240_id[] = { | |
452 | {"MMC35240", 0}, | |
453 | {} | |
454 | }; | |
455 | MODULE_DEVICE_TABLE(i2c, mmc35240_id); | |
456 | ||
457 | static struct i2c_driver mmc35240_driver = { | |
458 | .driver = { | |
459 | .name = MMC35240_DRV_NAME, | |
460 | }, | |
461 | .probe = mmc35240_probe, | |
462 | .id_table = mmc35240_id, | |
463 | }; | |
464 | ||
465 | module_i2c_driver(mmc35240_driver); | |
466 | ||
467 | MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>"); | |
468 | MODULE_DESCRIPTION("MEMSIC MMC35240 magnetic sensor driver"); | |
469 | MODULE_LICENSE("GPL v2"); |