tmp007.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * tmp007.c - Support for TI TMP007 IR thermopile sensor with integrated math engine
  3. *
  4. * Copyright (c) 2017 Manivannan Sadhasivam <manivannanece23@gmail.com>
  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. * Driver for the Texas Instruments I2C 16-bit IR thermopile sensor
  11. *
  12. * (7-bit I2C slave address (0x40 - 0x47), changeable via ADR pins)
  13. *
  14. * Note:
  15. * 1. This driver assumes that the sensor has been calibrated beforehand
  16. * 2. Limit threshold events are enabled at the start
  17. * 3. Operating mode: INT
  18. *
  19. */
  20. #include <linux/err.h>
  21. #include <linux/i2c.h>
  22. #include <linux/delay.h>
  23. #include <linux/module.h>
  24. #include <linux/pm.h>
  25. #include <linux/bitops.h>
  26. #include <linux/of.h>
  27. #include <linux/irq.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/iio/iio.h>
  30. #include <linux/iio/sysfs.h>
  31. #include <linux/iio/events.h>
  32. #define TMP007_TDIE 0x01
  33. #define TMP007_CONFIG 0x02
  34. #define TMP007_TOBJECT 0x03
  35. #define TMP007_STATUS 0x04
  36. #define TMP007_STATUS_MASK 0x05
  37. #define TMP007_TOBJ_HIGH_LIMIT 0x06
  38. #define TMP007_TOBJ_LOW_LIMIT 0x07
  39. #define TMP007_TDIE_HIGH_LIMIT 0x08
  40. #define TMP007_TDIE_LOW_LIMIT 0x09
  41. #define TMP007_MANUFACTURER_ID 0x1e
  42. #define TMP007_DEVICE_ID 0x1f
  43. #define TMP007_CONFIG_CONV_EN BIT(12)
  44. #define TMP007_CONFIG_TC_EN BIT(6)
  45. #define TMP007_CONFIG_CR_MASK GENMASK(11, 9)
  46. #define TMP007_CONFIG_ALERT_EN BIT(8)
  47. #define TMP007_CONFIG_CR_SHIFT 9
  48. /* Status register flags */
  49. #define TMP007_STATUS_ALERT BIT(15)
  50. #define TMP007_STATUS_CONV_READY BIT(14)
  51. #define TMP007_STATUS_OHF BIT(13)
  52. #define TMP007_STATUS_OLF BIT(12)
  53. #define TMP007_STATUS_LHF BIT(11)
  54. #define TMP007_STATUS_LLF BIT(10)
  55. #define TMP007_STATUS_DATA_VALID BIT(9)
  56. #define TMP007_MANUFACTURER_MAGIC 0x5449
  57. #define TMP007_DEVICE_MAGIC 0x0078
  58. #define TMP007_TEMP_SHIFT 2
  59. struct tmp007_data {
  60. struct i2c_client *client;
  61. struct mutex lock;
  62. u16 config;
  63. u16 status_mask;
  64. };
  65. static const int tmp007_avgs[5][2] = { {4, 0}, {2, 0}, {1, 0},
  66. {0, 500000}, {0, 250000} };
  67. static int tmp007_read_temperature(struct tmp007_data *data, u8 reg)
  68. {
  69. s32 ret;
  70. int tries = 50;
  71. while (tries-- > 0) {
  72. ret = i2c_smbus_read_word_swapped(data->client,
  73. TMP007_STATUS);
  74. if (ret < 0)
  75. return ret;
  76. if ((ret & TMP007_STATUS_CONV_READY) &&
  77. !(ret & TMP007_STATUS_DATA_VALID))
  78. break;
  79. msleep(100);
  80. }
  81. if (tries < 0)
  82. return -EIO;
  83. return i2c_smbus_read_word_swapped(data->client, reg);
  84. }
  85. static int tmp007_powerdown(struct tmp007_data *data)
  86. {
  87. return i2c_smbus_write_word_swapped(data->client, TMP007_CONFIG,
  88. data->config & ~TMP007_CONFIG_CONV_EN);
  89. }
  90. static int tmp007_read_raw(struct iio_dev *indio_dev,
  91. struct iio_chan_spec const *channel, int *val,
  92. int *val2, long mask)
  93. {
  94. struct tmp007_data *data = iio_priv(indio_dev);
  95. s32 ret;
  96. int conv_rate;
  97. switch (mask) {
  98. case IIO_CHAN_INFO_RAW:
  99. switch (channel->channel2) {
  100. case IIO_MOD_TEMP_AMBIENT: /* LSB: 0.03125 degree Celsius */
  101. ret = i2c_smbus_read_word_swapped(data->client, TMP007_TDIE);
  102. if (ret < 0)
  103. return ret;
  104. break;
  105. case IIO_MOD_TEMP_OBJECT:
  106. ret = tmp007_read_temperature(data, TMP007_TOBJECT);
  107. if (ret < 0)
  108. return ret;
  109. break;
  110. default:
  111. return -EINVAL;
  112. }
  113. *val = sign_extend32(ret, 15) >> TMP007_TEMP_SHIFT;
  114. return IIO_VAL_INT;
  115. case IIO_CHAN_INFO_SCALE:
  116. *val = 31;
  117. *val2 = 250000;
  118. return IIO_VAL_INT_PLUS_MICRO;
  119. case IIO_CHAN_INFO_SAMP_FREQ:
  120. conv_rate = (data->config & TMP007_CONFIG_CR_MASK)
  121. >> TMP007_CONFIG_CR_SHIFT;
  122. *val = tmp007_avgs[conv_rate][0];
  123. *val2 = tmp007_avgs[conv_rate][1];
  124. return IIO_VAL_INT_PLUS_MICRO;
  125. default:
  126. return -EINVAL;
  127. }
  128. }
  129. static int tmp007_write_raw(struct iio_dev *indio_dev,
  130. struct iio_chan_spec const *channel, int val,
  131. int val2, long mask)
  132. {
  133. struct tmp007_data *data = iio_priv(indio_dev);
  134. int i;
  135. u16 tmp;
  136. if (mask == IIO_CHAN_INFO_SAMP_FREQ) {
  137. for (i = 0; i < ARRAY_SIZE(tmp007_avgs); i++) {
  138. if ((val == tmp007_avgs[i][0]) &&
  139. (val2 == tmp007_avgs[i][1])) {
  140. tmp = data->config & ~TMP007_CONFIG_CR_MASK;
  141. tmp |= (i << TMP007_CONFIG_CR_SHIFT);
  142. return i2c_smbus_write_word_swapped(data->client,
  143. TMP007_CONFIG,
  144. data->config = tmp);
  145. }
  146. }
  147. }
  148. return -EINVAL;
  149. }
  150. static irqreturn_t tmp007_interrupt_handler(int irq, void *private)
  151. {
  152. struct iio_dev *indio_dev = private;
  153. struct tmp007_data *data = iio_priv(indio_dev);
  154. int ret;
  155. ret = i2c_smbus_read_word_swapped(data->client, TMP007_STATUS);
  156. if ((ret < 0) || !(ret & (TMP007_STATUS_OHF | TMP007_STATUS_OLF |
  157. TMP007_STATUS_LHF | TMP007_STATUS_LLF)))
  158. return IRQ_NONE;
  159. if (ret & TMP007_STATUS_OHF)
  160. iio_push_event(indio_dev,
  161. IIO_MOD_EVENT_CODE(IIO_TEMP, 0,
  162. IIO_MOD_TEMP_OBJECT,
  163. IIO_EV_TYPE_THRESH,
  164. IIO_EV_DIR_RISING),
  165. iio_get_time_ns(indio_dev));
  166. if (ret & TMP007_STATUS_OLF)
  167. iio_push_event(indio_dev,
  168. IIO_MOD_EVENT_CODE(IIO_TEMP, 0,
  169. IIO_MOD_TEMP_OBJECT,
  170. IIO_EV_TYPE_THRESH,
  171. IIO_EV_DIR_FALLING),
  172. iio_get_time_ns(indio_dev));
  173. if (ret & TMP007_STATUS_LHF)
  174. iio_push_event(indio_dev,
  175. IIO_MOD_EVENT_CODE(IIO_TEMP, 0,
  176. IIO_MOD_TEMP_AMBIENT,
  177. IIO_EV_TYPE_THRESH,
  178. IIO_EV_DIR_RISING),
  179. iio_get_time_ns(indio_dev));
  180. if (ret & TMP007_STATUS_LLF)
  181. iio_push_event(indio_dev,
  182. IIO_MOD_EVENT_CODE(IIO_TEMP, 0,
  183. IIO_MOD_TEMP_AMBIENT,
  184. IIO_EV_TYPE_THRESH,
  185. IIO_EV_DIR_FALLING),
  186. iio_get_time_ns(indio_dev));
  187. return IRQ_HANDLED;
  188. }
  189. static int tmp007_write_event_config(struct iio_dev *indio_dev,
  190. const struct iio_chan_spec *chan, enum iio_event_type type,
  191. enum iio_event_direction dir, int state)
  192. {
  193. struct tmp007_data *data = iio_priv(indio_dev);
  194. unsigned int status_mask;
  195. int ret;
  196. switch (chan->channel2) {
  197. case IIO_MOD_TEMP_AMBIENT:
  198. if (dir == IIO_EV_DIR_RISING)
  199. status_mask = TMP007_STATUS_LHF;
  200. else
  201. status_mask = TMP007_STATUS_LLF;
  202. break;
  203. case IIO_MOD_TEMP_OBJECT:
  204. if (dir == IIO_EV_DIR_RISING)
  205. status_mask = TMP007_STATUS_OHF;
  206. else
  207. status_mask = TMP007_STATUS_OLF;
  208. break;
  209. default:
  210. return -EINVAL;
  211. }
  212. mutex_lock(&data->lock);
  213. ret = i2c_smbus_read_word_swapped(data->client, TMP007_STATUS_MASK);
  214. mutex_unlock(&data->lock);
  215. if (ret < 0)
  216. return ret;
  217. if (state)
  218. ret |= status_mask;
  219. else
  220. ret &= ~status_mask;
  221. return i2c_smbus_write_word_swapped(data->client, TMP007_STATUS_MASK,
  222. data->status_mask = ret);
  223. }
  224. static int tmp007_read_event_config(struct iio_dev *indio_dev,
  225. const struct iio_chan_spec *chan, enum iio_event_type type,
  226. enum iio_event_direction dir)
  227. {
  228. struct tmp007_data *data = iio_priv(indio_dev);
  229. unsigned int mask;
  230. switch (chan->channel2) {
  231. case IIO_MOD_TEMP_AMBIENT:
  232. if (dir == IIO_EV_DIR_RISING)
  233. mask = TMP007_STATUS_LHF;
  234. else
  235. mask = TMP007_STATUS_LLF;
  236. break;
  237. case IIO_MOD_TEMP_OBJECT:
  238. if (dir == IIO_EV_DIR_RISING)
  239. mask = TMP007_STATUS_OHF;
  240. else
  241. mask = TMP007_STATUS_OLF;
  242. break;
  243. default:
  244. return -EINVAL;
  245. }
  246. return !!(data->status_mask & mask);
  247. }
  248. static int tmp007_read_thresh(struct iio_dev *indio_dev,
  249. const struct iio_chan_spec *chan, enum iio_event_type type,
  250. enum iio_event_direction dir, enum iio_event_info info,
  251. int *val, int *val2)
  252. {
  253. struct tmp007_data *data = iio_priv(indio_dev);
  254. int ret;
  255. u8 reg;
  256. switch (chan->channel2) {
  257. case IIO_MOD_TEMP_AMBIENT: /* LSB: 0.5 degree Celsius */
  258. if (dir == IIO_EV_DIR_RISING)
  259. reg = TMP007_TDIE_HIGH_LIMIT;
  260. else
  261. reg = TMP007_TDIE_LOW_LIMIT;
  262. break;
  263. case IIO_MOD_TEMP_OBJECT:
  264. if (dir == IIO_EV_DIR_RISING)
  265. reg = TMP007_TOBJ_HIGH_LIMIT;
  266. else
  267. reg = TMP007_TOBJ_LOW_LIMIT;
  268. break;
  269. default:
  270. return -EINVAL;
  271. }
  272. ret = i2c_smbus_read_word_swapped(data->client, reg);
  273. if (ret < 0)
  274. return ret;
  275. /* Shift length 7 bits = 6(15:6) + 1(0.5 LSB) */
  276. *val = sign_extend32(ret, 15) >> 7;
  277. return IIO_VAL_INT;
  278. }
  279. static int tmp007_write_thresh(struct iio_dev *indio_dev,
  280. const struct iio_chan_spec *chan, enum iio_event_type type,
  281. enum iio_event_direction dir, enum iio_event_info info,
  282. int val, int val2)
  283. {
  284. struct tmp007_data *data = iio_priv(indio_dev);
  285. u8 reg;
  286. switch (chan->channel2) {
  287. case IIO_MOD_TEMP_AMBIENT:
  288. if (dir == IIO_EV_DIR_RISING)
  289. reg = TMP007_TDIE_HIGH_LIMIT;
  290. else
  291. reg = TMP007_TDIE_LOW_LIMIT;
  292. break;
  293. case IIO_MOD_TEMP_OBJECT:
  294. if (dir == IIO_EV_DIR_RISING)
  295. reg = TMP007_TOBJ_HIGH_LIMIT;
  296. else
  297. reg = TMP007_TOBJ_LOW_LIMIT;
  298. break;
  299. default:
  300. return -EINVAL;
  301. }
  302. /* Full scale threshold value is +/- 256 degree Celsius */
  303. if (val < -256 || val > 255)
  304. return -EINVAL;
  305. /* Shift length 7 bits = 6(15:6) + 1(0.5 LSB) */
  306. return i2c_smbus_write_word_swapped(data->client, reg, (val << 7));
  307. }
  308. static IIO_CONST_ATTR(sampling_frequency_available, "4 2 1 0.5 0.25");
  309. static struct attribute *tmp007_attributes[] = {
  310. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  311. NULL
  312. };
  313. static const struct attribute_group tmp007_attribute_group = {
  314. .attrs = tmp007_attributes,
  315. };
  316. static const struct iio_event_spec tmp007_obj_event[] = {
  317. {
  318. .type = IIO_EV_TYPE_THRESH,
  319. .dir = IIO_EV_DIR_RISING,
  320. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  321. BIT(IIO_EV_INFO_ENABLE),
  322. },
  323. {
  324. .type = IIO_EV_TYPE_THRESH,
  325. .dir = IIO_EV_DIR_FALLING,
  326. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  327. BIT(IIO_EV_INFO_ENABLE),
  328. },
  329. };
  330. static const struct iio_event_spec tmp007_die_event[] = {
  331. {
  332. .type = IIO_EV_TYPE_THRESH,
  333. .dir = IIO_EV_DIR_RISING,
  334. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  335. BIT(IIO_EV_INFO_ENABLE),
  336. },
  337. {
  338. .type = IIO_EV_TYPE_THRESH,
  339. .dir = IIO_EV_DIR_FALLING,
  340. .mask_separate = BIT(IIO_EV_INFO_VALUE) |
  341. BIT(IIO_EV_INFO_ENABLE),
  342. },
  343. };
  344. static const struct iio_chan_spec tmp007_channels[] = {
  345. {
  346. .type = IIO_TEMP,
  347. .modified = 1,
  348. .channel2 = IIO_MOD_TEMP_AMBIENT,
  349. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  350. BIT(IIO_CHAN_INFO_SCALE),
  351. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  352. .event_spec = tmp007_die_event,
  353. .num_event_specs = ARRAY_SIZE(tmp007_die_event),
  354. },
  355. {
  356. .type = IIO_TEMP,
  357. .modified = 1,
  358. .channel2 = IIO_MOD_TEMP_OBJECT,
  359. .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  360. BIT(IIO_CHAN_INFO_SCALE),
  361. .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
  362. .event_spec = tmp007_obj_event,
  363. .num_event_specs = ARRAY_SIZE(tmp007_obj_event),
  364. }
  365. };
  366. static const struct iio_info tmp007_info = {
  367. .read_raw = tmp007_read_raw,
  368. .write_raw = tmp007_write_raw,
  369. .read_event_config = tmp007_read_event_config,
  370. .write_event_config = tmp007_write_event_config,
  371. .read_event_value = tmp007_read_thresh,
  372. .write_event_value = tmp007_write_thresh,
  373. .attrs = &tmp007_attribute_group,
  374. };
  375. static bool tmp007_identify(struct i2c_client *client)
  376. {
  377. int manf_id, dev_id;
  378. manf_id = i2c_smbus_read_word_swapped(client, TMP007_MANUFACTURER_ID);
  379. if (manf_id < 0)
  380. return false;
  381. dev_id = i2c_smbus_read_word_swapped(client, TMP007_DEVICE_ID);
  382. if (dev_id < 0)
  383. return false;
  384. return (manf_id == TMP007_MANUFACTURER_MAGIC && dev_id == TMP007_DEVICE_MAGIC);
  385. }
  386. static int tmp007_probe(struct i2c_client *client,
  387. const struct i2c_device_id *tmp007_id)
  388. {
  389. struct tmp007_data *data;
  390. struct iio_dev *indio_dev;
  391. int ret;
  392. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_WORD_DATA))
  393. return -EOPNOTSUPP;
  394. if (!tmp007_identify(client)) {
  395. dev_err(&client->dev, "TMP007 not found\n");
  396. return -ENODEV;
  397. }
  398. indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
  399. if (!indio_dev)
  400. return -ENOMEM;
  401. data = iio_priv(indio_dev);
  402. i2c_set_clientdata(client, indio_dev);
  403. data->client = client;
  404. mutex_init(&data->lock);
  405. indio_dev->dev.parent = &client->dev;
  406. indio_dev->name = "tmp007";
  407. indio_dev->modes = INDIO_DIRECT_MODE;
  408. indio_dev->info = &tmp007_info;
  409. indio_dev->channels = tmp007_channels;
  410. indio_dev->num_channels = ARRAY_SIZE(tmp007_channels);
  411. /*
  412. * Set Configuration register:
  413. * 1. Conversion ON
  414. * 2. ALERT enable
  415. * 3. Transient correction enable
  416. */
  417. ret = i2c_smbus_read_word_swapped(data->client, TMP007_CONFIG);
  418. if (ret < 0)
  419. return ret;
  420. data->config = ret;
  421. data->config |= (TMP007_CONFIG_CONV_EN | TMP007_CONFIG_ALERT_EN | TMP007_CONFIG_TC_EN);
  422. ret = i2c_smbus_write_word_swapped(data->client, TMP007_CONFIG,
  423. data->config);
  424. if (ret < 0)
  425. return ret;
  426. /*
  427. * Only the following flags can activate ALERT pin. Data conversion/validity flags
  428. * flags can still be polled for getting temperature data
  429. *
  430. * Set Status Mask register:
  431. * 1. Object temperature high limit enable
  432. * 2. Object temperature low limit enable
  433. * 3. TDIE temperature high limit enable
  434. * 4. TDIE temperature low limit enable
  435. */
  436. ret = i2c_smbus_read_word_swapped(data->client, TMP007_STATUS_MASK);
  437. if (ret < 0)
  438. goto error_powerdown;
  439. data->status_mask = ret;
  440. data->status_mask |= (TMP007_STATUS_OHF | TMP007_STATUS_OLF
  441. | TMP007_STATUS_LHF | TMP007_STATUS_LLF);
  442. ret = i2c_smbus_write_word_swapped(data->client, TMP007_STATUS_MASK, data->status_mask);
  443. if (ret < 0)
  444. goto error_powerdown;
  445. if (client->irq) {
  446. ret = devm_request_threaded_irq(&client->dev, client->irq,
  447. NULL, tmp007_interrupt_handler,
  448. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  449. tmp007_id->name, indio_dev);
  450. if (ret) {
  451. dev_err(&client->dev, "irq request error %d\n", -ret);
  452. goto error_powerdown;
  453. }
  454. }
  455. return iio_device_register(indio_dev);
  456. error_powerdown:
  457. tmp007_powerdown(data);
  458. return ret;
  459. }
  460. static int tmp007_remove(struct i2c_client *client)
  461. {
  462. struct iio_dev *indio_dev = i2c_get_clientdata(client);
  463. struct tmp007_data *data = iio_priv(indio_dev);
  464. iio_device_unregister(indio_dev);
  465. tmp007_powerdown(data);
  466. return 0;
  467. }
  468. #ifdef CONFIG_PM_SLEEP
  469. static int tmp007_suspend(struct device *dev)
  470. {
  471. struct tmp007_data *data = iio_priv(i2c_get_clientdata(
  472. to_i2c_client(dev)));
  473. return tmp007_powerdown(data);
  474. }
  475. static int tmp007_resume(struct device *dev)
  476. {
  477. struct tmp007_data *data = iio_priv(i2c_get_clientdata(
  478. to_i2c_client(dev)));
  479. return i2c_smbus_write_word_swapped(data->client, TMP007_CONFIG,
  480. data->config | TMP007_CONFIG_CONV_EN);
  481. }
  482. #endif
  483. static SIMPLE_DEV_PM_OPS(tmp007_pm_ops, tmp007_suspend, tmp007_resume);
  484. static const struct of_device_id tmp007_of_match[] = {
  485. { .compatible = "ti,tmp007", },
  486. { },
  487. };
  488. MODULE_DEVICE_TABLE(of, tmp007_of_match);
  489. static const struct i2c_device_id tmp007_id[] = {
  490. { "tmp007", 0 },
  491. { }
  492. };
  493. MODULE_DEVICE_TABLE(i2c, tmp007_id);
  494. static struct i2c_driver tmp007_driver = {
  495. .driver = {
  496. .name = "tmp007",
  497. .of_match_table = of_match_ptr(tmp007_of_match),
  498. .pm = &tmp007_pm_ops,
  499. },
  500. .probe = tmp007_probe,
  501. .remove = tmp007_remove,
  502. .id_table = tmp007_id,
  503. };
  504. module_i2c_driver(tmp007_driver);
  505. MODULE_AUTHOR("Manivannan Sadhasivam <manivannanece23@gmail.com>");
  506. MODULE_DESCRIPTION("TI TMP007 IR thermopile sensor driver");
  507. MODULE_LICENSE("GPL");