max30100.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * max30100.c - Support for MAX30100 heart rate and pulse oximeter sensor
  4. *
  5. * Copyright (C) 2015, 2018
  6. * Author: Matt Ranostay <matt.ranostay@konsulko.com>
  7. *
  8. * TODO: enable pulse length controls via device tree properties
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/irq.h>
  16. #include <linux/i2c.h>
  17. #include <linux/mutex.h>
  18. #include <linux/of.h>
  19. #include <linux/regmap.h>
  20. #include <linux/iio/iio.h>
  21. #include <linux/iio/buffer.h>
  22. #include <linux/iio/kfifo_buf.h>
  23. #define MAX30100_REGMAP_NAME "max30100_regmap"
  24. #define MAX30100_DRV_NAME "max30100"
  25. #define MAX30100_REG_INT_STATUS 0x00
  26. #define MAX30100_REG_INT_STATUS_PWR_RDY BIT(0)
  27. #define MAX30100_REG_INT_STATUS_SPO2_RDY BIT(4)
  28. #define MAX30100_REG_INT_STATUS_HR_RDY BIT(5)
  29. #define MAX30100_REG_INT_STATUS_FIFO_RDY BIT(7)
  30. #define MAX30100_REG_INT_ENABLE 0x01
  31. #define MAX30100_REG_INT_ENABLE_SPO2_EN BIT(0)
  32. #define MAX30100_REG_INT_ENABLE_HR_EN BIT(1)
  33. #define MAX30100_REG_INT_ENABLE_FIFO_EN BIT(3)
  34. #define MAX30100_REG_INT_ENABLE_MASK 0xf0
  35. #define MAX30100_REG_INT_ENABLE_MASK_SHIFT 4
  36. #define MAX30100_REG_FIFO_WR_PTR 0x02
  37. #define MAX30100_REG_FIFO_OVR_CTR 0x03
  38. #define MAX30100_REG_FIFO_RD_PTR 0x04
  39. #define MAX30100_REG_FIFO_DATA 0x05
  40. #define MAX30100_REG_FIFO_DATA_ENTRY_COUNT 16
  41. #define MAX30100_REG_FIFO_DATA_ENTRY_LEN 4
  42. #define MAX30100_REG_MODE_CONFIG 0x06
  43. #define MAX30100_REG_MODE_CONFIG_MODE_SPO2_EN BIT(0)
  44. #define MAX30100_REG_MODE_CONFIG_MODE_HR_EN BIT(1)
  45. #define MAX30100_REG_MODE_CONFIG_MODE_MASK 0x03
  46. #define MAX30100_REG_MODE_CONFIG_TEMP_EN BIT(3)
  47. #define MAX30100_REG_MODE_CONFIG_PWR BIT(7)
  48. #define MAX30100_REG_SPO2_CONFIG 0x07
  49. #define MAX30100_REG_SPO2_CONFIG_100HZ BIT(2)
  50. #define MAX30100_REG_SPO2_CONFIG_HI_RES_EN BIT(6)
  51. #define MAX30100_REG_SPO2_CONFIG_1600US 0x3
  52. #define MAX30100_REG_LED_CONFIG 0x09
  53. #define MAX30100_REG_LED_CONFIG_LED_MASK 0x0f
  54. #define MAX30100_REG_LED_CONFIG_RED_LED_SHIFT 4
  55. #define MAX30100_REG_LED_CONFIG_24MA 0x07
  56. #define MAX30100_REG_LED_CONFIG_50MA 0x0f
  57. #define MAX30100_REG_TEMP_INTEGER 0x16
  58. #define MAX30100_REG_TEMP_FRACTION 0x17
  59. struct max30100_data {
  60. struct i2c_client *client;
  61. struct iio_dev *indio_dev;
  62. struct mutex lock;
  63. struct regmap *regmap;
  64. __be16 buffer[2]; /* 2 16-bit channels */
  65. };
  66. static bool max30100_is_volatile_reg(struct device *dev, unsigned int reg)
  67. {
  68. switch (reg) {
  69. case MAX30100_REG_INT_STATUS:
  70. case MAX30100_REG_MODE_CONFIG:
  71. case MAX30100_REG_FIFO_WR_PTR:
  72. case MAX30100_REG_FIFO_OVR_CTR:
  73. case MAX30100_REG_FIFO_RD_PTR:
  74. case MAX30100_REG_FIFO_DATA:
  75. case MAX30100_REG_TEMP_INTEGER:
  76. case MAX30100_REG_TEMP_FRACTION:
  77. return true;
  78. default:
  79. return false;
  80. }
  81. }
  82. static const struct regmap_config max30100_regmap_config = {
  83. .name = MAX30100_REGMAP_NAME,
  84. .reg_bits = 8,
  85. .val_bits = 8,
  86. .max_register = MAX30100_REG_TEMP_FRACTION,
  87. .cache_type = REGCACHE_FLAT,
  88. .volatile_reg = max30100_is_volatile_reg,
  89. };
  90. static const unsigned int max30100_led_current_mapping[] = {
  91. 4400, 7600, 11000, 14200, 17400,
  92. 20800, 24000, 27100, 30600, 33800,
  93. 37000, 40200, 43600, 46800, 50000
  94. };
  95. static const unsigned long max30100_scan_masks[] = {0x3, 0};
  96. static const struct iio_chan_spec max30100_channels[] = {
  97. {
  98. .type = IIO_INTENSITY,
  99. .channel2 = IIO_MOD_LIGHT_IR,
  100. .modified = 1,
  101. .scan_index = 0,
  102. .scan_type = {
  103. .sign = 'u',
  104. .realbits = 16,
  105. .storagebits = 16,
  106. .endianness = IIO_BE,
  107. },
  108. },
  109. {
  110. .type = IIO_INTENSITY,
  111. .channel2 = IIO_MOD_LIGHT_RED,
  112. .modified = 1,
  113. .scan_index = 1,
  114. .scan_type = {
  115. .sign = 'u',
  116. .realbits = 16,
  117. .storagebits = 16,
  118. .endianness = IIO_BE,
  119. },
  120. },
  121. {
  122. .type = IIO_TEMP,
  123. .info_mask_separate =
  124. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  125. .scan_index = -1,
  126. },
  127. };
  128. static int max30100_set_powermode(struct max30100_data *data, bool state)
  129. {
  130. return regmap_update_bits(data->regmap, MAX30100_REG_MODE_CONFIG,
  131. MAX30100_REG_MODE_CONFIG_PWR,
  132. state ? 0 : MAX30100_REG_MODE_CONFIG_PWR);
  133. }
  134. static int max30100_clear_fifo(struct max30100_data *data)
  135. {
  136. int ret;
  137. ret = regmap_write(data->regmap, MAX30100_REG_FIFO_WR_PTR, 0);
  138. if (ret)
  139. return ret;
  140. ret = regmap_write(data->regmap, MAX30100_REG_FIFO_OVR_CTR, 0);
  141. if (ret)
  142. return ret;
  143. return regmap_write(data->regmap, MAX30100_REG_FIFO_RD_PTR, 0);
  144. }
  145. static int max30100_buffer_postenable(struct iio_dev *indio_dev)
  146. {
  147. struct max30100_data *data = iio_priv(indio_dev);
  148. int ret;
  149. ret = max30100_set_powermode(data, true);
  150. if (ret)
  151. return ret;
  152. return max30100_clear_fifo(data);
  153. }
  154. static int max30100_buffer_predisable(struct iio_dev *indio_dev)
  155. {
  156. struct max30100_data *data = iio_priv(indio_dev);
  157. return max30100_set_powermode(data, false);
  158. }
  159. static const struct iio_buffer_setup_ops max30100_buffer_setup_ops = {
  160. .postenable = max30100_buffer_postenable,
  161. .predisable = max30100_buffer_predisable,
  162. };
  163. static inline int max30100_fifo_count(struct max30100_data *data)
  164. {
  165. unsigned int val;
  166. int ret;
  167. ret = regmap_read(data->regmap, MAX30100_REG_INT_STATUS, &val);
  168. if (ret)
  169. return ret;
  170. /* FIFO is almost full */
  171. if (val & MAX30100_REG_INT_STATUS_FIFO_RDY)
  172. return MAX30100_REG_FIFO_DATA_ENTRY_COUNT - 1;
  173. return 0;
  174. }
  175. static int max30100_read_measurement(struct max30100_data *data)
  176. {
  177. int ret;
  178. ret = i2c_smbus_read_i2c_block_data(data->client,
  179. MAX30100_REG_FIFO_DATA,
  180. MAX30100_REG_FIFO_DATA_ENTRY_LEN,
  181. (u8 *) &data->buffer);
  182. return (ret == MAX30100_REG_FIFO_DATA_ENTRY_LEN) ? 0 : ret;
  183. }
  184. static irqreturn_t max30100_interrupt_handler(int irq, void *private)
  185. {
  186. struct iio_dev *indio_dev = private;
  187. struct max30100_data *data = iio_priv(indio_dev);
  188. int ret, cnt = 0;
  189. mutex_lock(&data->lock);
  190. while (cnt || (cnt = max30100_fifo_count(data)) > 0) {
  191. ret = max30100_read_measurement(data);
  192. if (ret)
  193. break;
  194. iio_push_to_buffers(data->indio_dev, data->buffer);
  195. cnt--;
  196. }
  197. mutex_unlock(&data->lock);
  198. return IRQ_HANDLED;
  199. }
  200. static int max30100_get_current_idx(unsigned int val, int *reg)
  201. {
  202. int idx;
  203. /* LED turned off */
  204. if (val == 0) {
  205. *reg = 0;
  206. return 0;
  207. }
  208. for (idx = 0; idx < ARRAY_SIZE(max30100_led_current_mapping); idx++) {
  209. if (max30100_led_current_mapping[idx] == val) {
  210. *reg = idx + 1;
  211. return 0;
  212. }
  213. }
  214. return -EINVAL;
  215. }
  216. static int max30100_led_init(struct max30100_data *data)
  217. {
  218. struct device *dev = &data->client->dev;
  219. struct device_node *np = dev->of_node;
  220. unsigned int val[2];
  221. int reg, ret;
  222. ret = of_property_read_u32_array(np, "maxim,led-current-microamp",
  223. (unsigned int *) &val, 2);
  224. if (ret) {
  225. /* Default to 24 mA RED LED, 50 mA IR LED */
  226. reg = (MAX30100_REG_LED_CONFIG_24MA <<
  227. MAX30100_REG_LED_CONFIG_RED_LED_SHIFT) |
  228. MAX30100_REG_LED_CONFIG_50MA;
  229. dev_warn(dev, "no led-current-microamp set");
  230. return regmap_write(data->regmap, MAX30100_REG_LED_CONFIG, reg);
  231. }
  232. /* RED LED current */
  233. ret = max30100_get_current_idx(val[0], &reg);
  234. if (ret) {
  235. dev_err(dev, "invalid RED current setting %d", val[0]);
  236. return ret;
  237. }
  238. ret = regmap_update_bits(data->regmap, MAX30100_REG_LED_CONFIG,
  239. MAX30100_REG_LED_CONFIG_LED_MASK <<
  240. MAX30100_REG_LED_CONFIG_RED_LED_SHIFT,
  241. reg << MAX30100_REG_LED_CONFIG_RED_LED_SHIFT);
  242. if (ret)
  243. return ret;
  244. /* IR LED current */
  245. ret = max30100_get_current_idx(val[1], &reg);
  246. if (ret) {
  247. dev_err(dev, "invalid IR current setting %d", val[1]);
  248. return ret;
  249. }
  250. return regmap_update_bits(data->regmap, MAX30100_REG_LED_CONFIG,
  251. MAX30100_REG_LED_CONFIG_LED_MASK, reg);
  252. }
  253. static int max30100_chip_init(struct max30100_data *data)
  254. {
  255. int ret;
  256. /* setup LED current settings */
  257. ret = max30100_led_init(data);
  258. if (ret)
  259. return ret;
  260. /* enable hi-res SPO2 readings at 100Hz */
  261. ret = regmap_write(data->regmap, MAX30100_REG_SPO2_CONFIG,
  262. MAX30100_REG_SPO2_CONFIG_HI_RES_EN |
  263. MAX30100_REG_SPO2_CONFIG_100HZ);
  264. if (ret)
  265. return ret;
  266. /* enable SPO2 mode */
  267. ret = regmap_update_bits(data->regmap, MAX30100_REG_MODE_CONFIG,
  268. MAX30100_REG_MODE_CONFIG_MODE_MASK,
  269. MAX30100_REG_MODE_CONFIG_MODE_HR_EN |
  270. MAX30100_REG_MODE_CONFIG_MODE_SPO2_EN);
  271. if (ret)
  272. return ret;
  273. /* enable FIFO interrupt */
  274. return regmap_update_bits(data->regmap, MAX30100_REG_INT_ENABLE,
  275. MAX30100_REG_INT_ENABLE_MASK,
  276. MAX30100_REG_INT_ENABLE_FIFO_EN
  277. << MAX30100_REG_INT_ENABLE_MASK_SHIFT);
  278. }
  279. static int max30100_read_temp(struct max30100_data *data, int *val)
  280. {
  281. int ret;
  282. unsigned int reg;
  283. ret = regmap_read(data->regmap, MAX30100_REG_TEMP_INTEGER, &reg);
  284. if (ret < 0)
  285. return ret;
  286. *val = reg << 4;
  287. ret = regmap_read(data->regmap, MAX30100_REG_TEMP_FRACTION, &reg);
  288. if (ret < 0)
  289. return ret;
  290. *val |= reg & 0xf;
  291. *val = sign_extend32(*val, 11);
  292. return 0;
  293. }
  294. static int max30100_get_temp(struct max30100_data *data, int *val)
  295. {
  296. int ret;
  297. /* start acquisition */
  298. ret = regmap_update_bits(data->regmap, MAX30100_REG_MODE_CONFIG,
  299. MAX30100_REG_MODE_CONFIG_TEMP_EN,
  300. MAX30100_REG_MODE_CONFIG_TEMP_EN);
  301. if (ret)
  302. return ret;
  303. msleep(35);
  304. return max30100_read_temp(data, val);
  305. }
  306. static int max30100_read_raw(struct iio_dev *indio_dev,
  307. struct iio_chan_spec const *chan,
  308. int *val, int *val2, long mask)
  309. {
  310. struct max30100_data *data = iio_priv(indio_dev);
  311. int ret = -EINVAL;
  312. switch (mask) {
  313. case IIO_CHAN_INFO_RAW:
  314. /*
  315. * Temperature reading can only be acquired while engine
  316. * is running
  317. */
  318. mutex_lock(&indio_dev->mlock);
  319. if (!iio_buffer_enabled(indio_dev))
  320. ret = -EAGAIN;
  321. else {
  322. ret = max30100_get_temp(data, val);
  323. if (!ret)
  324. ret = IIO_VAL_INT;
  325. }
  326. mutex_unlock(&indio_dev->mlock);
  327. break;
  328. case IIO_CHAN_INFO_SCALE:
  329. *val = 1; /* 0.0625 */
  330. *val2 = 16;
  331. ret = IIO_VAL_FRACTIONAL;
  332. break;
  333. }
  334. return ret;
  335. }
  336. static const struct iio_info max30100_info = {
  337. .read_raw = max30100_read_raw,
  338. };
  339. static int max30100_probe(struct i2c_client *client,
  340. const struct i2c_device_id *id)
  341. {
  342. struct max30100_data *data;
  343. struct iio_buffer *buffer;
  344. struct iio_dev *indio_dev;
  345. int ret;
  346. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  347. if (!indio_dev)
  348. return -ENOMEM;
  349. buffer = devm_iio_kfifo_allocate(&client->dev);
  350. if (!buffer)
  351. return -ENOMEM;
  352. iio_device_attach_buffer(indio_dev, buffer);
  353. indio_dev->name = MAX30100_DRV_NAME;
  354. indio_dev->channels = max30100_channels;
  355. indio_dev->info = &max30100_info;
  356. indio_dev->num_channels = ARRAY_SIZE(max30100_channels);
  357. indio_dev->available_scan_masks = max30100_scan_masks;
  358. indio_dev->modes = (INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE);
  359. indio_dev->setup_ops = &max30100_buffer_setup_ops;
  360. indio_dev->dev.parent = &client->dev;
  361. data = iio_priv(indio_dev);
  362. data->indio_dev = indio_dev;
  363. data->client = client;
  364. mutex_init(&data->lock);
  365. i2c_set_clientdata(client, indio_dev);
  366. data->regmap = devm_regmap_init_i2c(client, &max30100_regmap_config);
  367. if (IS_ERR(data->regmap)) {
  368. dev_err(&client->dev, "regmap initialization failed.\n");
  369. return PTR_ERR(data->regmap);
  370. }
  371. max30100_set_powermode(data, false);
  372. ret = max30100_chip_init(data);
  373. if (ret)
  374. return ret;
  375. if (client->irq <= 0) {
  376. dev_err(&client->dev, "no valid irq defined\n");
  377. return -EINVAL;
  378. }
  379. ret = devm_request_threaded_irq(&client->dev, client->irq,
  380. NULL, max30100_interrupt_handler,
  381. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  382. "max30100_irq", indio_dev);
  383. if (ret) {
  384. dev_err(&client->dev, "request irq (%d) failed\n", client->irq);
  385. return ret;
  386. }
  387. return iio_device_register(indio_dev);
  388. }
  389. static int max30100_remove(struct i2c_client *client)
  390. {
  391. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  392. struct max30100_data *data = iio_priv(indio_dev);
  393. iio_device_unregister(indio_dev);
  394. max30100_set_powermode(data, false);
  395. return 0;
  396. }
  397. static const struct i2c_device_id max30100_id[] = {
  398. { "max30100", 0 },
  399. {}
  400. };
  401. MODULE_DEVICE_TABLE(i2c, max30100_id);
  402. static const struct of_device_id max30100_dt_ids[] = {
  403. { .compatible = "maxim,max30100" },
  404. { }
  405. };
  406. MODULE_DEVICE_TABLE(of, max30100_dt_ids);
  407. static struct i2c_driver max30100_driver = {
  408. .driver = {
  409. .name = MAX30100_DRV_NAME,
  410. .of_match_table = of_match_ptr(max30100_dt_ids),
  411. },
  412. .probe = max30100_probe,
  413. .remove = max30100_remove,
  414. .id_table = max30100_id,
  415. };
  416. module_i2c_driver(max30100_driver);
  417. MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
  418. MODULE_DESCRIPTION("MAX30100 heart rate and pulse oximeter sensor");
  419. MODULE_LICENSE("GPL");