mmc35240.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/i2c.h>
  17. #include <linux/delay.h>
  18. #include <linux/regmap.h>
  19. #include <linux/acpi.h>
  20. #include <linux/pm.h>
  21. #include <linux/iio/iio.h>
  22. #include <linux/iio/sysfs.h>
  23. #define MMC35240_DRV_NAME "mmc35240"
  24. #define MMC35240_REGMAP_NAME "mmc35240_regmap"
  25. #define MMC35240_REG_XOUT_L 0x00
  26. #define MMC35240_REG_XOUT_H 0x01
  27. #define MMC35240_REG_YOUT_L 0x02
  28. #define MMC35240_REG_YOUT_H 0x03
  29. #define MMC35240_REG_ZOUT_L 0x04
  30. #define MMC35240_REG_ZOUT_H 0x05
  31. #define MMC35240_REG_STATUS 0x06
  32. #define MMC35240_REG_CTRL0 0x07
  33. #define MMC35240_REG_CTRL1 0x08
  34. #define MMC35240_REG_ID 0x20
  35. #define MMC35240_STATUS_MEAS_DONE_BIT BIT(0)
  36. #define MMC35240_CTRL0_REFILL_BIT BIT(7)
  37. #define MMC35240_CTRL0_RESET_BIT BIT(6)
  38. #define MMC35240_CTRL0_SET_BIT BIT(5)
  39. #define MMC35240_CTRL0_CMM_BIT BIT(1)
  40. #define MMC35240_CTRL0_TM_BIT BIT(0)
  41. /* output resolution bits */
  42. #define MMC35240_CTRL1_BW0_BIT BIT(0)
  43. #define MMC35240_CTRL1_BW1_BIT BIT(1)
  44. #define MMC35240_CTRL1_BW_MASK (MMC35240_CTRL1_BW0_BIT | \
  45. MMC35240_CTRL1_BW1_BIT)
  46. #define MMC35240_CTRL1_BW_SHIFT 0
  47. #define MMC35240_WAIT_CHARGE_PUMP 50000 /* us */
  48. #define MMC53240_WAIT_SET_RESET 1000 /* us */
  49. /*
  50. * Memsic OTP process code piece is put here for reference:
  51. *
  52. * #define OTP_CONVERT(REG) ((float)((REG) >=32 ? (32 - (REG)) : (REG)) * 0.006
  53. * 1) For X axis, the COEFFICIENT is always 1.
  54. * 2) For Y axis, the COEFFICIENT is as below:
  55. * f_OTP_matrix[4] = OTP_CONVERT(((reg_data[1] & 0x03) << 4) |
  56. * (reg_data[2] >> 4)) + 1.0;
  57. * 3) For Z axis, the COEFFICIENT is as below:
  58. * f_OTP_matrix[8] = (OTP_CONVERT(reg_data[3] & 0x3f) + 1) * 1.35;
  59. * We implemented the OTP logic into driver.
  60. */
  61. /* scale = 1000 here for Y otp */
  62. #define MMC35240_OTP_CONVERT_Y(REG) (((REG) >= 32 ? (32 - (REG)) : (REG)) * 6)
  63. /* 0.6 * 1.35 = 0.81, scale 10000 for Z otp */
  64. #define MMC35240_OTP_CONVERT_Z(REG) (((REG) >= 32 ? (32 - (REG)) : (REG)) * 81)
  65. #define MMC35240_X_COEFF(x) (x)
  66. #define MMC35240_Y_COEFF(y) (y + 1000)
  67. #define MMC35240_Z_COEFF(z) (z + 13500)
  68. #define MMC35240_OTP_START_ADDR 0x1B
  69. enum mmc35240_resolution {
  70. MMC35240_16_BITS_SLOW = 0, /* 7.92 ms */
  71. MMC35240_16_BITS_FAST, /* 4.08 ms */
  72. MMC35240_14_BITS, /* 2.16 ms */
  73. MMC35240_12_BITS, /* 1.20 ms */
  74. };
  75. enum mmc35240_axis {
  76. AXIS_X = 0,
  77. AXIS_Y,
  78. AXIS_Z,
  79. };
  80. static const struct {
  81. int sens[3]; /* sensitivity per X, Y, Z axis */
  82. int nfo; /* null field output */
  83. } mmc35240_props_table[] = {
  84. /* 16 bits, 125Hz ODR */
  85. {
  86. {1024, 1024, 1024},
  87. 32768,
  88. },
  89. /* 16 bits, 250Hz ODR */
  90. {
  91. {1024, 1024, 770},
  92. 32768,
  93. },
  94. /* 14 bits, 450Hz ODR */
  95. {
  96. {256, 256, 193},
  97. 8192,
  98. },
  99. /* 12 bits, 800Hz ODR */
  100. {
  101. {64, 64, 48},
  102. 2048,
  103. },
  104. };
  105. struct mmc35240_data {
  106. struct i2c_client *client;
  107. struct mutex mutex;
  108. struct regmap *regmap;
  109. enum mmc35240_resolution res;
  110. /* OTP compensation */
  111. int axis_coef[3];
  112. int axis_scale[3];
  113. };
  114. static const struct {
  115. int val;
  116. int val2;
  117. } mmc35240_samp_freq[] = { {1, 500000},
  118. {13, 0},
  119. {25, 0},
  120. {50, 0} };
  121. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1.5 13 25 50");
  122. #define MMC35240_CHANNEL(_axis) { \
  123. .type = IIO_MAGN, \
  124. .modified = 1, \
  125. .channel2 = IIO_MOD_ ## _axis, \
  126. .address = AXIS_ ## _axis, \
  127. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
  128. .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
  129. BIT(IIO_CHAN_INFO_SCALE), \
  130. }
  131. static const struct iio_chan_spec mmc35240_channels[] = {
  132. MMC35240_CHANNEL(X),
  133. MMC35240_CHANNEL(Y),
  134. MMC35240_CHANNEL(Z),
  135. };
  136. static struct attribute *mmc35240_attributes[] = {
  137. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  138. NULL
  139. };
  140. static const struct attribute_group mmc35240_attribute_group = {
  141. .attrs = mmc35240_attributes,
  142. };
  143. static int mmc35240_get_samp_freq_index(struct mmc35240_data *data,
  144. int val, int val2)
  145. {
  146. int i;
  147. for (i = 0; i < ARRAY_SIZE(mmc35240_samp_freq); i++)
  148. if (mmc35240_samp_freq[i].val == val &&
  149. mmc35240_samp_freq[i].val2 == val2)
  150. return i;
  151. return -EINVAL;
  152. }
  153. static int mmc35240_hw_set(struct mmc35240_data *data, bool set)
  154. {
  155. int ret;
  156. u8 coil_bit;
  157. /*
  158. * Recharge the capacitor at VCAP pin, requested to be issued
  159. * before a SET/RESET command.
  160. */
  161. ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL0,
  162. MMC35240_CTRL0_REFILL_BIT,
  163. MMC35240_CTRL0_REFILL_BIT);
  164. if (ret < 0)
  165. return ret;
  166. usleep_range(MMC35240_WAIT_CHARGE_PUMP, MMC35240_WAIT_CHARGE_PUMP + 1);
  167. if (set)
  168. coil_bit = MMC35240_CTRL0_SET_BIT;
  169. else
  170. coil_bit = MMC35240_CTRL0_RESET_BIT;
  171. return regmap_update_bits(data->regmap, MMC35240_REG_CTRL0,
  172. coil_bit, coil_bit);
  173. }
  174. static int mmc35240_init(struct mmc35240_data *data)
  175. {
  176. int ret, y_convert, z_convert;
  177. unsigned int reg_id;
  178. u8 otp_data[6];
  179. ret = regmap_read(data->regmap, MMC35240_REG_ID, &reg_id);
  180. if (ret < 0) {
  181. dev_err(&data->client->dev, "Error reading product id\n");
  182. return ret;
  183. }
  184. dev_dbg(&data->client->dev, "MMC35240 chip id %x\n", reg_id);
  185. /*
  186. * make sure we restore sensor characteristics, by doing
  187. * a SET/RESET sequence, the axis polarity being naturally
  188. * aligned after RESET
  189. */
  190. ret = mmc35240_hw_set(data, true);
  191. if (ret < 0)
  192. return ret;
  193. usleep_range(MMC53240_WAIT_SET_RESET, MMC53240_WAIT_SET_RESET + 1);
  194. ret = mmc35240_hw_set(data, false);
  195. if (ret < 0)
  196. return ret;
  197. /* set default sampling frequency */
  198. ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL1,
  199. MMC35240_CTRL1_BW_MASK,
  200. data->res << MMC35240_CTRL1_BW_SHIFT);
  201. if (ret < 0)
  202. return ret;
  203. ret = regmap_bulk_read(data->regmap, MMC35240_OTP_START_ADDR,
  204. (u8 *)otp_data, sizeof(otp_data));
  205. if (ret < 0)
  206. return ret;
  207. y_convert = MMC35240_OTP_CONVERT_Y(((otp_data[1] & 0x03) << 4) |
  208. (otp_data[2] >> 4));
  209. z_convert = MMC35240_OTP_CONVERT_Z(otp_data[3] & 0x3f);
  210. data->axis_coef[0] = MMC35240_X_COEFF(1);
  211. data->axis_coef[1] = MMC35240_Y_COEFF(y_convert);
  212. data->axis_coef[2] = MMC35240_Z_COEFF(z_convert);
  213. data->axis_scale[0] = 1;
  214. data->axis_scale[1] = 1000;
  215. data->axis_scale[2] = 10000;
  216. return 0;
  217. }
  218. static int mmc35240_take_measurement(struct mmc35240_data *data)
  219. {
  220. int ret, tries = 100;
  221. unsigned int reg_status;
  222. ret = regmap_write(data->regmap, MMC35240_REG_CTRL0,
  223. MMC35240_CTRL0_TM_BIT);
  224. if (ret < 0)
  225. return ret;
  226. while (tries-- > 0) {
  227. ret = regmap_read(data->regmap, MMC35240_REG_STATUS,
  228. &reg_status);
  229. if (ret < 0)
  230. return ret;
  231. if (reg_status & MMC35240_STATUS_MEAS_DONE_BIT)
  232. break;
  233. /* minimum wait time to complete measurement is 10 ms */
  234. usleep_range(10000, 11000);
  235. }
  236. if (tries < 0) {
  237. dev_err(&data->client->dev, "data not ready\n");
  238. return -EIO;
  239. }
  240. return 0;
  241. }
  242. static int mmc35240_read_measurement(struct mmc35240_data *data, __le16 buf[3])
  243. {
  244. int ret;
  245. ret = mmc35240_take_measurement(data);
  246. if (ret < 0)
  247. return ret;
  248. return regmap_bulk_read(data->regmap, MMC35240_REG_XOUT_L, (u8 *)buf,
  249. 3 * sizeof(__le16));
  250. }
  251. /**
  252. * mmc35240_raw_to_mgauss - convert raw readings to milli gauss. Also apply
  253. compensation for output value.
  254. *
  255. * @data: device private data
  256. * @index: axis index for which we want the conversion
  257. * @buf: raw data to be converted, 2 bytes in little endian format
  258. * @val: compensated output reading (unit is milli gauss)
  259. *
  260. * Returns: 0 in case of success, -EINVAL when @index is not valid
  261. */
  262. static int mmc35240_raw_to_mgauss(struct mmc35240_data *data, int index,
  263. __le16 buf[], int *val)
  264. {
  265. int raw[3];
  266. int sens[3];
  267. int nfo;
  268. raw[AXIS_X] = le16_to_cpu(buf[AXIS_X]);
  269. raw[AXIS_Y] = le16_to_cpu(buf[AXIS_Y]);
  270. raw[AXIS_Z] = le16_to_cpu(buf[AXIS_Z]);
  271. sens[AXIS_X] = mmc35240_props_table[data->res].sens[AXIS_X];
  272. sens[AXIS_Y] = mmc35240_props_table[data->res].sens[AXIS_Y];
  273. sens[AXIS_Z] = mmc35240_props_table[data->res].sens[AXIS_Z];
  274. nfo = mmc35240_props_table[data->res].nfo;
  275. switch (index) {
  276. case AXIS_X:
  277. *val = (raw[AXIS_X] - nfo) * 1000 / sens[AXIS_X];
  278. break;
  279. case AXIS_Y:
  280. *val = (raw[AXIS_Y] - nfo) * 1000 / sens[AXIS_Y] -
  281. (raw[AXIS_Z] - nfo) * 1000 / sens[AXIS_Z];
  282. break;
  283. case AXIS_Z:
  284. *val = (raw[AXIS_Y] - nfo) * 1000 / sens[AXIS_Y] +
  285. (raw[AXIS_Z] - nfo) * 1000 / sens[AXIS_Z];
  286. break;
  287. default:
  288. return -EINVAL;
  289. }
  290. /* apply OTP compensation */
  291. *val = (*val) * data->axis_coef[index] / data->axis_scale[index];
  292. return 0;
  293. }
  294. static int mmc35240_read_raw(struct iio_dev *indio_dev,
  295. struct iio_chan_spec const *chan, int *val,
  296. int *val2, long mask)
  297. {
  298. struct mmc35240_data *data = iio_priv(indio_dev);
  299. int ret, i;
  300. unsigned int reg;
  301. __le16 buf[3];
  302. switch (mask) {
  303. case IIO_CHAN_INFO_RAW:
  304. mutex_lock(&data->mutex);
  305. ret = mmc35240_read_measurement(data, buf);
  306. mutex_unlock(&data->mutex);
  307. if (ret < 0)
  308. return ret;
  309. ret = mmc35240_raw_to_mgauss(data, chan->address, buf, val);
  310. if (ret < 0)
  311. return ret;
  312. return IIO_VAL_INT;
  313. case IIO_CHAN_INFO_SCALE:
  314. *val = 0;
  315. *val2 = 1000;
  316. return IIO_VAL_INT_PLUS_MICRO;
  317. case IIO_CHAN_INFO_SAMP_FREQ:
  318. mutex_lock(&data->mutex);
  319. ret = regmap_read(data->regmap, MMC35240_REG_CTRL1, &reg);
  320. mutex_unlock(&data->mutex);
  321. if (ret < 0)
  322. return ret;
  323. i = (reg & MMC35240_CTRL1_BW_MASK) >> MMC35240_CTRL1_BW_SHIFT;
  324. if (i < 0 || i >= ARRAY_SIZE(mmc35240_samp_freq))
  325. return -EINVAL;
  326. *val = mmc35240_samp_freq[i].val;
  327. *val2 = mmc35240_samp_freq[i].val2;
  328. return IIO_VAL_INT_PLUS_MICRO;
  329. default:
  330. return -EINVAL;
  331. }
  332. }
  333. static int mmc35240_write_raw(struct iio_dev *indio_dev,
  334. struct iio_chan_spec const *chan, int val,
  335. int val2, long mask)
  336. {
  337. struct mmc35240_data *data = iio_priv(indio_dev);
  338. int i, ret;
  339. switch (mask) {
  340. case IIO_CHAN_INFO_SAMP_FREQ:
  341. i = mmc35240_get_samp_freq_index(data, val, val2);
  342. if (i < 0)
  343. return -EINVAL;
  344. mutex_lock(&data->mutex);
  345. ret = regmap_update_bits(data->regmap, MMC35240_REG_CTRL1,
  346. MMC35240_CTRL1_BW_MASK,
  347. i << MMC35240_CTRL1_BW_SHIFT);
  348. mutex_unlock(&data->mutex);
  349. return ret;
  350. default:
  351. return -EINVAL;
  352. }
  353. }
  354. static const struct iio_info mmc35240_info = {
  355. .driver_module = THIS_MODULE,
  356. .read_raw = mmc35240_read_raw,
  357. .write_raw = mmc35240_write_raw,
  358. .attrs = &mmc35240_attribute_group,
  359. };
  360. static bool mmc35240_is_writeable_reg(struct device *dev, unsigned int reg)
  361. {
  362. switch (reg) {
  363. case MMC35240_REG_CTRL0:
  364. case MMC35240_REG_CTRL1:
  365. return true;
  366. default:
  367. return false;
  368. }
  369. }
  370. static bool mmc35240_is_readable_reg(struct device *dev, unsigned int reg)
  371. {
  372. switch (reg) {
  373. case MMC35240_REG_XOUT_L:
  374. case MMC35240_REG_XOUT_H:
  375. case MMC35240_REG_YOUT_L:
  376. case MMC35240_REG_YOUT_H:
  377. case MMC35240_REG_ZOUT_L:
  378. case MMC35240_REG_ZOUT_H:
  379. case MMC35240_REG_STATUS:
  380. case MMC35240_REG_ID:
  381. return true;
  382. default:
  383. return false;
  384. }
  385. }
  386. static bool mmc35240_is_volatile_reg(struct device *dev, unsigned int reg)
  387. {
  388. switch (reg) {
  389. case MMC35240_REG_CTRL0:
  390. case MMC35240_REG_CTRL1:
  391. return false;
  392. default:
  393. return true;
  394. }
  395. }
  396. static struct reg_default mmc35240_reg_defaults[] = {
  397. { MMC35240_REG_CTRL0, 0x00 },
  398. { MMC35240_REG_CTRL1, 0x00 },
  399. };
  400. static const struct regmap_config mmc35240_regmap_config = {
  401. .name = MMC35240_REGMAP_NAME,
  402. .reg_bits = 8,
  403. .val_bits = 8,
  404. .max_register = MMC35240_REG_ID,
  405. .cache_type = REGCACHE_FLAT,
  406. .writeable_reg = mmc35240_is_writeable_reg,
  407. .readable_reg = mmc35240_is_readable_reg,
  408. .volatile_reg = mmc35240_is_volatile_reg,
  409. .reg_defaults = mmc35240_reg_defaults,
  410. .num_reg_defaults = ARRAY_SIZE(mmc35240_reg_defaults),
  411. };
  412. static int mmc35240_probe(struct i2c_client *client,
  413. const struct i2c_device_id *id)
  414. {
  415. struct mmc35240_data *data;
  416. struct iio_dev *indio_dev;
  417. struct regmap *regmap;
  418. int ret;
  419. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  420. if (!indio_dev)
  421. return -ENOMEM;
  422. regmap = devm_regmap_init_i2c(client, &mmc35240_regmap_config);
  423. if (IS_ERR(regmap)) {
  424. dev_err(&client->dev, "regmap initialization failed\n");
  425. return PTR_ERR(regmap);
  426. }
  427. data = iio_priv(indio_dev);
  428. i2c_set_clientdata(client, indio_dev);
  429. data->client = client;
  430. data->regmap = regmap;
  431. data->res = MMC35240_16_BITS_SLOW;
  432. mutex_init(&data->mutex);
  433. indio_dev->dev.parent = &client->dev;
  434. indio_dev->info = &mmc35240_info;
  435. indio_dev->name = MMC35240_DRV_NAME;
  436. indio_dev->channels = mmc35240_channels;
  437. indio_dev->num_channels = ARRAY_SIZE(mmc35240_channels);
  438. indio_dev->modes = INDIO_DIRECT_MODE;
  439. ret = mmc35240_init(data);
  440. if (ret < 0) {
  441. dev_err(&client->dev, "mmc35240 chip init failed\n");
  442. return ret;
  443. }
  444. return devm_iio_device_register(&client->dev, indio_dev);
  445. }
  446. #ifdef CONFIG_PM_SLEEP
  447. static int mmc35240_suspend(struct device *dev)
  448. {
  449. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  450. struct mmc35240_data *data = iio_priv(indio_dev);
  451. regcache_cache_only(data->regmap, true);
  452. return 0;
  453. }
  454. static int mmc35240_resume(struct device *dev)
  455. {
  456. struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
  457. struct mmc35240_data *data = iio_priv(indio_dev);
  458. int ret;
  459. regcache_mark_dirty(data->regmap);
  460. ret = regcache_sync_region(data->regmap, MMC35240_REG_CTRL0,
  461. MMC35240_REG_CTRL1);
  462. if (ret < 0)
  463. dev_err(dev, "Failed to restore control registers\n");
  464. regcache_cache_only(data->regmap, false);
  465. return 0;
  466. }
  467. #endif
  468. static const struct dev_pm_ops mmc35240_pm_ops = {
  469. SET_SYSTEM_SLEEP_PM_OPS(mmc35240_suspend, mmc35240_resume)
  470. };
  471. static const struct of_device_id mmc35240_of_match[] = {
  472. { .compatible = "memsic,mmc35240", },
  473. { }
  474. };
  475. MODULE_DEVICE_TABLE(of, mmc35240_of_match);
  476. static const struct acpi_device_id mmc35240_acpi_match[] = {
  477. {"MMC35240", 0},
  478. { },
  479. };
  480. MODULE_DEVICE_TABLE(acpi, mmc35240_acpi_match);
  481. static const struct i2c_device_id mmc35240_id[] = {
  482. {"mmc35240", 0},
  483. {}
  484. };
  485. MODULE_DEVICE_TABLE(i2c, mmc35240_id);
  486. static struct i2c_driver mmc35240_driver = {
  487. .driver = {
  488. .name = MMC35240_DRV_NAME,
  489. .of_match_table = mmc35240_of_match,
  490. .pm = &mmc35240_pm_ops,
  491. .acpi_match_table = ACPI_PTR(mmc35240_acpi_match),
  492. },
  493. .probe = mmc35240_probe,
  494. .id_table = mmc35240_id,
  495. };
  496. module_i2c_driver(mmc35240_driver);
  497. MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
  498. MODULE_DESCRIPTION("MEMSIC MMC35240 magnetic sensor driver");
  499. MODULE_LICENSE("GPL v2");