sht15.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * sht15.c - support for the SHT15 Temperature and Humidity Sensor
  4. *
  5. * Portions Copyright (c) 2010-2012 Savoir-faire Linux Inc.
  6. * Jerome Oufella <jerome.oufella@savoirfairelinux.com>
  7. * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
  8. *
  9. * Copyright (c) 2009 Jonathan Cameron
  10. *
  11. * Copyright (c) 2007 Wouter Horre
  12. *
  13. * For further information, see the Documentation/hwmon/sht15.rst file.
  14. */
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/hwmon.h>
  20. #include <linux/hwmon-sysfs.h>
  21. #include <linux/mutex.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/sched.h>
  24. #include <linux/delay.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/err.h>
  27. #include <linux/regulator/consumer.h>
  28. #include <linux/slab.h>
  29. #include <linux/atomic.h>
  30. #include <linux/bitrev.h>
  31. #include <linux/gpio/consumer.h>
  32. #include <linux/of.h>
  33. /* Commands */
  34. #define SHT15_MEASURE_TEMP 0x03
  35. #define SHT15_MEASURE_RH 0x05
  36. #define SHT15_WRITE_STATUS 0x06
  37. #define SHT15_READ_STATUS 0x07
  38. #define SHT15_SOFT_RESET 0x1E
  39. /* Min timings */
  40. #define SHT15_TSCKL 100 /* (nsecs) clock low */
  41. #define SHT15_TSCKH 100 /* (nsecs) clock high */
  42. #define SHT15_TSU 150 /* (nsecs) data setup time */
  43. #define SHT15_TSRST 11 /* (msecs) soft reset time */
  44. /* Status Register Bits */
  45. #define SHT15_STATUS_LOW_RESOLUTION 0x01
  46. #define SHT15_STATUS_NO_OTP_RELOAD 0x02
  47. #define SHT15_STATUS_HEATER 0x04
  48. #define SHT15_STATUS_LOW_BATTERY 0x40
  49. /* List of supported chips */
  50. enum sht15_chips { sht10, sht11, sht15, sht71, sht75 };
  51. /* Actions the driver may be doing */
  52. enum sht15_state {
  53. SHT15_READING_NOTHING,
  54. SHT15_READING_TEMP,
  55. SHT15_READING_HUMID
  56. };
  57. /**
  58. * struct sht15_temppair - elements of voltage dependent temp calc
  59. * @vdd: supply voltage in microvolts
  60. * @d1: see data sheet
  61. */
  62. struct sht15_temppair {
  63. int vdd; /* microvolts */
  64. int d1;
  65. };
  66. /* Table 9 from datasheet - relates temperature calculation to supply voltage */
  67. static const struct sht15_temppair temppoints[] = {
  68. { 2500000, -39400 },
  69. { 3000000, -39600 },
  70. { 3500000, -39700 },
  71. { 4000000, -39800 },
  72. { 5000000, -40100 },
  73. };
  74. /* Table from CRC datasheet, section 2.4 */
  75. static const u8 sht15_crc8_table[] = {
  76. 0, 49, 98, 83, 196, 245, 166, 151,
  77. 185, 136, 219, 234, 125, 76, 31, 46,
  78. 67, 114, 33, 16, 135, 182, 229, 212,
  79. 250, 203, 152, 169, 62, 15, 92, 109,
  80. 134, 183, 228, 213, 66, 115, 32, 17,
  81. 63, 14, 93, 108, 251, 202, 153, 168,
  82. 197, 244, 167, 150, 1, 48, 99, 82,
  83. 124, 77, 30, 47, 184, 137, 218, 235,
  84. 61, 12, 95, 110, 249, 200, 155, 170,
  85. 132, 181, 230, 215, 64, 113, 34, 19,
  86. 126, 79, 28, 45, 186, 139, 216, 233,
  87. 199, 246, 165, 148, 3, 50, 97, 80,
  88. 187, 138, 217, 232, 127, 78, 29, 44,
  89. 2, 51, 96, 81, 198, 247, 164, 149,
  90. 248, 201, 154, 171, 60, 13, 94, 111,
  91. 65, 112, 35, 18, 133, 180, 231, 214,
  92. 122, 75, 24, 41, 190, 143, 220, 237,
  93. 195, 242, 161, 144, 7, 54, 101, 84,
  94. 57, 8, 91, 106, 253, 204, 159, 174,
  95. 128, 177, 226, 211, 68, 117, 38, 23,
  96. 252, 205, 158, 175, 56, 9, 90, 107,
  97. 69, 116, 39, 22, 129, 176, 227, 210,
  98. 191, 142, 221, 236, 123, 74, 25, 40,
  99. 6, 55, 100, 85, 194, 243, 160, 145,
  100. 71, 118, 37, 20, 131, 178, 225, 208,
  101. 254, 207, 156, 173, 58, 11, 88, 105,
  102. 4, 53, 102, 87, 192, 241, 162, 147,
  103. 189, 140, 223, 238, 121, 72, 27, 42,
  104. 193, 240, 163, 146, 5, 52, 103, 86,
  105. 120, 73, 26, 43, 188, 141, 222, 239,
  106. 130, 179, 224, 209, 70, 119, 36, 21,
  107. 59, 10, 89, 104, 255, 206, 157, 172
  108. };
  109. /**
  110. * struct sht15_data - device instance specific data
  111. * @sck: clock GPIO line
  112. * @data: data GPIO line
  113. * @read_work: bh of interrupt handler.
  114. * @wait_queue: wait queue for getting values from device.
  115. * @val_temp: last temperature value read from device.
  116. * @val_humid: last humidity value read from device.
  117. * @val_status: last status register value read from device.
  118. * @checksum_ok: last value read from the device passed CRC validation.
  119. * @checksumming: flag used to enable the data validation with CRC.
  120. * @state: state identifying the action the driver is doing.
  121. * @measurements_valid: are the current stored measures valid (start condition).
  122. * @status_valid: is the current stored status valid (start condition).
  123. * @last_measurement: time of last measure.
  124. * @last_status: time of last status reading.
  125. * @read_lock: mutex to ensure only one read in progress at a time.
  126. * @dev: associate device structure.
  127. * @hwmon_dev: device associated with hwmon subsystem.
  128. * @reg: associated regulator (if specified).
  129. * @nb: notifier block to handle notifications of voltage
  130. * changes.
  131. * @supply_uv: local copy of supply voltage used to allow use of
  132. * regulator consumer if available.
  133. * @supply_uv_valid: indicates that an updated value has not yet been
  134. * obtained from the regulator and so any calculations
  135. * based upon it will be invalid.
  136. * @update_supply_work: work struct that is used to update the supply_uv.
  137. * @interrupt_handled: flag used to indicate a handler has been scheduled.
  138. */
  139. struct sht15_data {
  140. struct gpio_desc *sck;
  141. struct gpio_desc *data;
  142. struct work_struct read_work;
  143. wait_queue_head_t wait_queue;
  144. uint16_t val_temp;
  145. uint16_t val_humid;
  146. u8 val_status;
  147. bool checksum_ok;
  148. bool checksumming;
  149. enum sht15_state state;
  150. bool measurements_valid;
  151. bool status_valid;
  152. unsigned long last_measurement;
  153. unsigned long last_status;
  154. struct mutex read_lock;
  155. struct device *dev;
  156. struct device *hwmon_dev;
  157. struct regulator *reg;
  158. struct notifier_block nb;
  159. int supply_uv;
  160. bool supply_uv_valid;
  161. struct work_struct update_supply_work;
  162. atomic_t interrupt_handled;
  163. };
  164. /**
  165. * sht15_crc8() - compute crc8
  166. * @data: sht15 specific data.
  167. * @value: sht15 retrieved data.
  168. * @len: Length of retrieved data
  169. *
  170. * This implements section 2 of the CRC datasheet.
  171. */
  172. static u8 sht15_crc8(struct sht15_data *data,
  173. const u8 *value,
  174. int len)
  175. {
  176. u8 crc = bitrev8(data->val_status & 0x0F);
  177. while (len--) {
  178. crc = sht15_crc8_table[*value ^ crc];
  179. value++;
  180. }
  181. return crc;
  182. }
  183. /**
  184. * sht15_connection_reset() - reset the comms interface
  185. * @data: sht15 specific data
  186. *
  187. * This implements section 3.4 of the data sheet
  188. */
  189. static int sht15_connection_reset(struct sht15_data *data)
  190. {
  191. int i, err;
  192. err = gpiod_direction_output(data->data, 1);
  193. if (err)
  194. return err;
  195. ndelay(SHT15_TSCKL);
  196. gpiod_set_value(data->sck, 0);
  197. ndelay(SHT15_TSCKL);
  198. for (i = 0; i < 9; ++i) {
  199. gpiod_set_value(data->sck, 1);
  200. ndelay(SHT15_TSCKH);
  201. gpiod_set_value(data->sck, 0);
  202. ndelay(SHT15_TSCKL);
  203. }
  204. return 0;
  205. }
  206. /**
  207. * sht15_send_bit() - send an individual bit to the device
  208. * @data: device state data
  209. * @val: value of bit to be sent
  210. */
  211. static inline void sht15_send_bit(struct sht15_data *data, int val)
  212. {
  213. gpiod_set_value(data->data, val);
  214. ndelay(SHT15_TSU);
  215. gpiod_set_value(data->sck, 1);
  216. ndelay(SHT15_TSCKH);
  217. gpiod_set_value(data->sck, 0);
  218. ndelay(SHT15_TSCKL); /* clock low time */
  219. }
  220. /**
  221. * sht15_transmission_start() - specific sequence for new transmission
  222. * @data: device state data
  223. *
  224. * Timings for this are not documented on the data sheet, so very
  225. * conservative ones used in implementation. This implements
  226. * figure 12 on the data sheet.
  227. */
  228. static int sht15_transmission_start(struct sht15_data *data)
  229. {
  230. int err;
  231. /* ensure data is high and output */
  232. err = gpiod_direction_output(data->data, 1);
  233. if (err)
  234. return err;
  235. ndelay(SHT15_TSU);
  236. gpiod_set_value(data->sck, 0);
  237. ndelay(SHT15_TSCKL);
  238. gpiod_set_value(data->sck, 1);
  239. ndelay(SHT15_TSCKH);
  240. gpiod_set_value(data->data, 0);
  241. ndelay(SHT15_TSU);
  242. gpiod_set_value(data->sck, 0);
  243. ndelay(SHT15_TSCKL);
  244. gpiod_set_value(data->sck, 1);
  245. ndelay(SHT15_TSCKH);
  246. gpiod_set_value(data->data, 1);
  247. ndelay(SHT15_TSU);
  248. gpiod_set_value(data->sck, 0);
  249. ndelay(SHT15_TSCKL);
  250. return 0;
  251. }
  252. /**
  253. * sht15_send_byte() - send a single byte to the device
  254. * @data: device state
  255. * @byte: value to be sent
  256. */
  257. static void sht15_send_byte(struct sht15_data *data, u8 byte)
  258. {
  259. int i;
  260. for (i = 0; i < 8; i++) {
  261. sht15_send_bit(data, !!(byte & 0x80));
  262. byte <<= 1;
  263. }
  264. }
  265. /**
  266. * sht15_wait_for_response() - checks for ack from device
  267. * @data: device state
  268. */
  269. static int sht15_wait_for_response(struct sht15_data *data)
  270. {
  271. int err;
  272. err = gpiod_direction_input(data->data);
  273. if (err)
  274. return err;
  275. gpiod_set_value(data->sck, 1);
  276. ndelay(SHT15_TSCKH);
  277. if (gpiod_get_value(data->data)) {
  278. gpiod_set_value(data->sck, 0);
  279. dev_err(data->dev, "Command not acknowledged\n");
  280. err = sht15_connection_reset(data);
  281. if (err)
  282. return err;
  283. return -EIO;
  284. }
  285. gpiod_set_value(data->sck, 0);
  286. ndelay(SHT15_TSCKL);
  287. return 0;
  288. }
  289. /**
  290. * sht15_send_cmd() - Sends a command to the device.
  291. * @data: device state
  292. * @cmd: command byte to be sent
  293. *
  294. * On entry, sck is output low, data is output pull high
  295. * and the interrupt disabled.
  296. */
  297. static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
  298. {
  299. int err;
  300. err = sht15_transmission_start(data);
  301. if (err)
  302. return err;
  303. sht15_send_byte(data, cmd);
  304. return sht15_wait_for_response(data);
  305. }
  306. /**
  307. * sht15_soft_reset() - send a soft reset command
  308. * @data: sht15 specific data.
  309. *
  310. * As described in section 3.2 of the datasheet.
  311. */
  312. static int sht15_soft_reset(struct sht15_data *data)
  313. {
  314. int ret;
  315. ret = sht15_send_cmd(data, SHT15_SOFT_RESET);
  316. if (ret)
  317. return ret;
  318. msleep(SHT15_TSRST);
  319. /* device resets default hardware status register value */
  320. data->val_status = 0;
  321. return ret;
  322. }
  323. /**
  324. * sht15_ack() - send a ack
  325. * @data: sht15 specific data.
  326. *
  327. * Each byte of data is acknowledged by pulling the data line
  328. * low for one clock pulse.
  329. */
  330. static int sht15_ack(struct sht15_data *data)
  331. {
  332. int err;
  333. err = gpiod_direction_output(data->data, 0);
  334. if (err)
  335. return err;
  336. ndelay(SHT15_TSU);
  337. gpiod_set_value(data->sck, 1);
  338. ndelay(SHT15_TSU);
  339. gpiod_set_value(data->sck, 0);
  340. ndelay(SHT15_TSU);
  341. gpiod_set_value(data->data, 1);
  342. return gpiod_direction_input(data->data);
  343. }
  344. /**
  345. * sht15_end_transmission() - notify device of end of transmission
  346. * @data: device state.
  347. *
  348. * This is basically a NAK (single clock pulse, data high).
  349. */
  350. static int sht15_end_transmission(struct sht15_data *data)
  351. {
  352. int err;
  353. err = gpiod_direction_output(data->data, 1);
  354. if (err)
  355. return err;
  356. ndelay(SHT15_TSU);
  357. gpiod_set_value(data->sck, 1);
  358. ndelay(SHT15_TSCKH);
  359. gpiod_set_value(data->sck, 0);
  360. ndelay(SHT15_TSCKL);
  361. return 0;
  362. }
  363. /**
  364. * sht15_read_byte() - Read a byte back from the device
  365. * @data: device state.
  366. */
  367. static u8 sht15_read_byte(struct sht15_data *data)
  368. {
  369. int i;
  370. u8 byte = 0;
  371. for (i = 0; i < 8; ++i) {
  372. byte <<= 1;
  373. gpiod_set_value(data->sck, 1);
  374. ndelay(SHT15_TSCKH);
  375. byte |= !!gpiod_get_value(data->data);
  376. gpiod_set_value(data->sck, 0);
  377. ndelay(SHT15_TSCKL);
  378. }
  379. return byte;
  380. }
  381. /**
  382. * sht15_send_status() - write the status register byte
  383. * @data: sht15 specific data.
  384. * @status: the byte to set the status register with.
  385. *
  386. * As described in figure 14 and table 5 of the datasheet.
  387. */
  388. static int sht15_send_status(struct sht15_data *data, u8 status)
  389. {
  390. int err;
  391. err = sht15_send_cmd(data, SHT15_WRITE_STATUS);
  392. if (err)
  393. return err;
  394. err = gpiod_direction_output(data->data, 1);
  395. if (err)
  396. return err;
  397. ndelay(SHT15_TSU);
  398. sht15_send_byte(data, status);
  399. err = sht15_wait_for_response(data);
  400. if (err)
  401. return err;
  402. data->val_status = status;
  403. return 0;
  404. }
  405. /**
  406. * sht15_update_status() - get updated status register from device if too old
  407. * @data: device instance specific data.
  408. *
  409. * As described in figure 15 and table 5 of the datasheet.
  410. */
  411. static int sht15_update_status(struct sht15_data *data)
  412. {
  413. int ret = 0;
  414. u8 status;
  415. u8 previous_config;
  416. u8 dev_checksum = 0;
  417. u8 checksum_vals[2];
  418. int timeout = HZ;
  419. mutex_lock(&data->read_lock);
  420. if (time_after(jiffies, data->last_status + timeout)
  421. || !data->status_valid) {
  422. ret = sht15_send_cmd(data, SHT15_READ_STATUS);
  423. if (ret)
  424. goto unlock;
  425. status = sht15_read_byte(data);
  426. if (data->checksumming) {
  427. sht15_ack(data);
  428. dev_checksum = bitrev8(sht15_read_byte(data));
  429. checksum_vals[0] = SHT15_READ_STATUS;
  430. checksum_vals[1] = status;
  431. data->checksum_ok = (sht15_crc8(data, checksum_vals, 2)
  432. == dev_checksum);
  433. }
  434. ret = sht15_end_transmission(data);
  435. if (ret)
  436. goto unlock;
  437. /*
  438. * Perform checksum validation on the received data.
  439. * Specification mentions that in case a checksum verification
  440. * fails, a soft reset command must be sent to the device.
  441. */
  442. if (data->checksumming && !data->checksum_ok) {
  443. previous_config = data->val_status & 0x07;
  444. ret = sht15_soft_reset(data);
  445. if (ret)
  446. goto unlock;
  447. if (previous_config) {
  448. ret = sht15_send_status(data, previous_config);
  449. if (ret) {
  450. dev_err(data->dev,
  451. "CRC validation failed, unable "
  452. "to restore device settings\n");
  453. goto unlock;
  454. }
  455. }
  456. ret = -EAGAIN;
  457. goto unlock;
  458. }
  459. data->val_status = status;
  460. data->status_valid = true;
  461. data->last_status = jiffies;
  462. }
  463. unlock:
  464. mutex_unlock(&data->read_lock);
  465. return ret;
  466. }
  467. /**
  468. * sht15_measurement() - get a new value from device
  469. * @data: device instance specific data
  470. * @command: command sent to request value
  471. * @timeout_msecs: timeout after which comms are assumed
  472. * to have failed are reset.
  473. */
  474. static int sht15_measurement(struct sht15_data *data,
  475. int command,
  476. int timeout_msecs)
  477. {
  478. int ret;
  479. u8 previous_config;
  480. ret = sht15_send_cmd(data, command);
  481. if (ret)
  482. return ret;
  483. ret = gpiod_direction_input(data->data);
  484. if (ret)
  485. return ret;
  486. atomic_set(&data->interrupt_handled, 0);
  487. enable_irq(gpiod_to_irq(data->data));
  488. if (gpiod_get_value(data->data) == 0) {
  489. disable_irq_nosync(gpiod_to_irq(data->data));
  490. /* Only relevant if the interrupt hasn't occurred. */
  491. if (!atomic_read(&data->interrupt_handled))
  492. schedule_work(&data->read_work);
  493. }
  494. ret = wait_event_timeout(data->wait_queue,
  495. (data->state == SHT15_READING_NOTHING),
  496. msecs_to_jiffies(timeout_msecs));
  497. if (data->state != SHT15_READING_NOTHING) { /* I/O error occurred */
  498. data->state = SHT15_READING_NOTHING;
  499. return -EIO;
  500. } else if (ret == 0) { /* timeout occurred */
  501. disable_irq_nosync(gpiod_to_irq(data->data));
  502. ret = sht15_connection_reset(data);
  503. if (ret)
  504. return ret;
  505. return -ETIME;
  506. }
  507. /*
  508. * Perform checksum validation on the received data.
  509. * Specification mentions that in case a checksum verification fails,
  510. * a soft reset command must be sent to the device.
  511. */
  512. if (data->checksumming && !data->checksum_ok) {
  513. previous_config = data->val_status & 0x07;
  514. ret = sht15_soft_reset(data);
  515. if (ret)
  516. return ret;
  517. if (previous_config) {
  518. ret = sht15_send_status(data, previous_config);
  519. if (ret) {
  520. dev_err(data->dev,
  521. "CRC validation failed, unable "
  522. "to restore device settings\n");
  523. return ret;
  524. }
  525. }
  526. return -EAGAIN;
  527. }
  528. return 0;
  529. }
  530. /**
  531. * sht15_update_measurements() - get updated measures from device if too old
  532. * @data: device state
  533. */
  534. static int sht15_update_measurements(struct sht15_data *data)
  535. {
  536. int ret = 0;
  537. int timeout = HZ;
  538. mutex_lock(&data->read_lock);
  539. if (time_after(jiffies, data->last_measurement + timeout)
  540. || !data->measurements_valid) {
  541. data->state = SHT15_READING_HUMID;
  542. ret = sht15_measurement(data, SHT15_MEASURE_RH, 160);
  543. if (ret)
  544. goto unlock;
  545. data->state = SHT15_READING_TEMP;
  546. ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
  547. if (ret)
  548. goto unlock;
  549. data->measurements_valid = true;
  550. data->last_measurement = jiffies;
  551. }
  552. unlock:
  553. mutex_unlock(&data->read_lock);
  554. return ret;
  555. }
  556. /**
  557. * sht15_calc_temp() - convert the raw reading to a temperature
  558. * @data: device state
  559. *
  560. * As per section 4.3 of the data sheet.
  561. */
  562. static inline int sht15_calc_temp(struct sht15_data *data)
  563. {
  564. int d1 = temppoints[0].d1;
  565. int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10;
  566. int i;
  567. for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
  568. /* Find pointer to interpolate */
  569. if (data->supply_uv > temppoints[i - 1].vdd) {
  570. d1 = (data->supply_uv - temppoints[i - 1].vdd)
  571. * (temppoints[i].d1 - temppoints[i - 1].d1)
  572. / (temppoints[i].vdd - temppoints[i - 1].vdd)
  573. + temppoints[i - 1].d1;
  574. break;
  575. }
  576. return data->val_temp * d2 + d1;
  577. }
  578. /**
  579. * sht15_calc_humid() - using last temperature convert raw to humid
  580. * @data: device state
  581. *
  582. * This is the temperature compensated version as per section 4.2 of
  583. * the data sheet.
  584. *
  585. * The sensor is assumed to be V3, which is compatible with V4.
  586. * Humidity conversion coefficients are shown in table 7 of the datasheet.
  587. */
  588. static inline int sht15_calc_humid(struct sht15_data *data)
  589. {
  590. int rh_linear; /* milli percent */
  591. int temp = sht15_calc_temp(data);
  592. int c2, c3;
  593. int t2;
  594. const int c1 = -4;
  595. if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) {
  596. c2 = 648000; /* x 10 ^ -6 */
  597. c3 = -7200; /* x 10 ^ -7 */
  598. t2 = 1280;
  599. } else {
  600. c2 = 40500; /* x 10 ^ -6 */
  601. c3 = -28; /* x 10 ^ -7 */
  602. t2 = 80;
  603. }
  604. rh_linear = c1 * 1000
  605. + c2 * data->val_humid / 1000
  606. + (data->val_humid * data->val_humid * c3) / 10000;
  607. return (temp - 25000) * (10000 + t2 * data->val_humid)
  608. / 1000000 + rh_linear;
  609. }
  610. /**
  611. * sht15_status_show() - show status information in sysfs
  612. * @dev: device.
  613. * @attr: device attribute.
  614. * @buf: sysfs buffer where information is written to.
  615. *
  616. * Will be called on read access to temp1_fault, humidity1_fault
  617. * and heater_enable sysfs attributes.
  618. * Returns number of bytes written into buffer, negative errno on error.
  619. */
  620. static ssize_t sht15_status_show(struct device *dev,
  621. struct device_attribute *attr, char *buf)
  622. {
  623. int ret;
  624. struct sht15_data *data = dev_get_drvdata(dev);
  625. u8 bit = to_sensor_dev_attr(attr)->index;
  626. ret = sht15_update_status(data);
  627. return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit));
  628. }
  629. /**
  630. * sht15_status_store() - change heater state via sysfs
  631. * @dev: device.
  632. * @attr: device attribute.
  633. * @buf: sysfs buffer to read the new heater state from.
  634. * @count: length of the data.
  635. *
  636. * Will be called on write access to heater_enable sysfs attribute.
  637. * Returns number of bytes actually decoded, negative errno on error.
  638. */
  639. static ssize_t sht15_status_store(struct device *dev,
  640. struct device_attribute *attr,
  641. const char *buf, size_t count)
  642. {
  643. int ret;
  644. struct sht15_data *data = dev_get_drvdata(dev);
  645. long value;
  646. u8 status;
  647. if (kstrtol(buf, 10, &value))
  648. return -EINVAL;
  649. mutex_lock(&data->read_lock);
  650. status = data->val_status & 0x07;
  651. if (!!value)
  652. status |= SHT15_STATUS_HEATER;
  653. else
  654. status &= ~SHT15_STATUS_HEATER;
  655. ret = sht15_send_status(data, status);
  656. mutex_unlock(&data->read_lock);
  657. return ret ? ret : count;
  658. }
  659. /**
  660. * sht15_temp_show() - show temperature measurement value in sysfs
  661. * @dev: device.
  662. * @attr: device attribute.
  663. * @buf: sysfs buffer where measurement values are written to.
  664. *
  665. * Will be called on read access to temp1_input sysfs attribute.
  666. * Returns number of bytes written into buffer, negative errno on error.
  667. */
  668. static ssize_t sht15_temp_show(struct device *dev,
  669. struct device_attribute *attr, char *buf)
  670. {
  671. int ret;
  672. struct sht15_data *data = dev_get_drvdata(dev);
  673. /* Technically no need to read humidity as well */
  674. ret = sht15_update_measurements(data);
  675. return ret ? ret : sprintf(buf, "%d\n",
  676. sht15_calc_temp(data));
  677. }
  678. /**
  679. * sht15_humidity_show() - show humidity measurement value in sysfs
  680. * @dev: device.
  681. * @attr: device attribute.
  682. * @buf: sysfs buffer where measurement values are written to.
  683. *
  684. * Will be called on read access to humidity1_input sysfs attribute.
  685. * Returns number of bytes written into buffer, negative errno on error.
  686. */
  687. static ssize_t sht15_humidity_show(struct device *dev,
  688. struct device_attribute *attr, char *buf)
  689. {
  690. int ret;
  691. struct sht15_data *data = dev_get_drvdata(dev);
  692. ret = sht15_update_measurements(data);
  693. return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
  694. }
  695. static ssize_t name_show(struct device *dev,
  696. struct device_attribute *attr,
  697. char *buf)
  698. {
  699. struct platform_device *pdev = to_platform_device(dev);
  700. return sprintf(buf, "%s\n", pdev->name);
  701. }
  702. static SENSOR_DEVICE_ATTR_RO(temp1_input, sht15_temp, 0);
  703. static SENSOR_DEVICE_ATTR_RO(humidity1_input, sht15_humidity, 0);
  704. static SENSOR_DEVICE_ATTR_RO(temp1_fault, sht15_status,
  705. SHT15_STATUS_LOW_BATTERY);
  706. static SENSOR_DEVICE_ATTR_RO(humidity1_fault, sht15_status,
  707. SHT15_STATUS_LOW_BATTERY);
  708. static SENSOR_DEVICE_ATTR_RW(heater_enable, sht15_status, SHT15_STATUS_HEATER);
  709. static DEVICE_ATTR_RO(name);
  710. static struct attribute *sht15_attrs[] = {
  711. &sensor_dev_attr_temp1_input.dev_attr.attr,
  712. &sensor_dev_attr_humidity1_input.dev_attr.attr,
  713. &sensor_dev_attr_temp1_fault.dev_attr.attr,
  714. &sensor_dev_attr_humidity1_fault.dev_attr.attr,
  715. &sensor_dev_attr_heater_enable.dev_attr.attr,
  716. &dev_attr_name.attr,
  717. NULL,
  718. };
  719. static const struct attribute_group sht15_attr_group = {
  720. .attrs = sht15_attrs,
  721. };
  722. static irqreturn_t sht15_interrupt_fired(int irq, void *d)
  723. {
  724. struct sht15_data *data = d;
  725. /* First disable the interrupt */
  726. disable_irq_nosync(irq);
  727. atomic_inc(&data->interrupt_handled);
  728. /* Then schedule a reading work struct */
  729. if (data->state != SHT15_READING_NOTHING)
  730. schedule_work(&data->read_work);
  731. return IRQ_HANDLED;
  732. }
  733. static void sht15_bh_read_data(struct work_struct *work_s)
  734. {
  735. uint16_t val = 0;
  736. u8 dev_checksum = 0;
  737. u8 checksum_vals[3];
  738. struct sht15_data *data
  739. = container_of(work_s, struct sht15_data,
  740. read_work);
  741. /* Firstly, verify the line is low */
  742. if (gpiod_get_value(data->data)) {
  743. /*
  744. * If not, then start the interrupt again - care here as could
  745. * have gone low in meantime so verify it hasn't!
  746. */
  747. atomic_set(&data->interrupt_handled, 0);
  748. enable_irq(gpiod_to_irq(data->data));
  749. /* If still not occurred or another handler was scheduled */
  750. if (gpiod_get_value(data->data)
  751. || atomic_read(&data->interrupt_handled))
  752. return;
  753. }
  754. /* Read the data back from the device */
  755. val = sht15_read_byte(data);
  756. val <<= 8;
  757. if (sht15_ack(data))
  758. goto wakeup;
  759. val |= sht15_read_byte(data);
  760. if (data->checksumming) {
  761. /*
  762. * Ask the device for a checksum and read it back.
  763. * Note: the device sends the checksum byte reversed.
  764. */
  765. if (sht15_ack(data))
  766. goto wakeup;
  767. dev_checksum = bitrev8(sht15_read_byte(data));
  768. checksum_vals[0] = (data->state == SHT15_READING_TEMP) ?
  769. SHT15_MEASURE_TEMP : SHT15_MEASURE_RH;
  770. checksum_vals[1] = (u8) (val >> 8);
  771. checksum_vals[2] = (u8) val;
  772. data->checksum_ok
  773. = (sht15_crc8(data, checksum_vals, 3) == dev_checksum);
  774. }
  775. /* Tell the device we are done */
  776. if (sht15_end_transmission(data))
  777. goto wakeup;
  778. switch (data->state) {
  779. case SHT15_READING_TEMP:
  780. data->val_temp = val;
  781. break;
  782. case SHT15_READING_HUMID:
  783. data->val_humid = val;
  784. break;
  785. default:
  786. break;
  787. }
  788. data->state = SHT15_READING_NOTHING;
  789. wakeup:
  790. wake_up(&data->wait_queue);
  791. }
  792. static void sht15_update_voltage(struct work_struct *work_s)
  793. {
  794. struct sht15_data *data
  795. = container_of(work_s, struct sht15_data,
  796. update_supply_work);
  797. data->supply_uv = regulator_get_voltage(data->reg);
  798. }
  799. /**
  800. * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
  801. * @nb: associated notification structure
  802. * @event: voltage regulator state change event code
  803. * @ignored: function parameter - ignored here
  804. *
  805. * Note that as the notification code holds the regulator lock, we have
  806. * to schedule an update of the supply voltage rather than getting it directly.
  807. */
  808. static int sht15_invalidate_voltage(struct notifier_block *nb,
  809. unsigned long event,
  810. void *ignored)
  811. {
  812. struct sht15_data *data = container_of(nb, struct sht15_data, nb);
  813. if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
  814. data->supply_uv_valid = false;
  815. schedule_work(&data->update_supply_work);
  816. return NOTIFY_OK;
  817. }
  818. #ifdef CONFIG_OF
  819. static const struct of_device_id sht15_dt_match[] = {
  820. { .compatible = "sensirion,sht15" },
  821. { },
  822. };
  823. MODULE_DEVICE_TABLE(of, sht15_dt_match);
  824. #endif
  825. static int sht15_probe(struct platform_device *pdev)
  826. {
  827. int ret;
  828. struct sht15_data *data;
  829. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  830. if (!data)
  831. return -ENOMEM;
  832. INIT_WORK(&data->read_work, sht15_bh_read_data);
  833. INIT_WORK(&data->update_supply_work, sht15_update_voltage);
  834. platform_set_drvdata(pdev, data);
  835. mutex_init(&data->read_lock);
  836. data->dev = &pdev->dev;
  837. init_waitqueue_head(&data->wait_queue);
  838. /*
  839. * If a regulator is available,
  840. * query what the supply voltage actually is!
  841. */
  842. data->reg = devm_regulator_get_optional(data->dev, "vcc");
  843. if (!IS_ERR(data->reg)) {
  844. int voltage;
  845. voltage = regulator_get_voltage(data->reg);
  846. if (voltage)
  847. data->supply_uv = voltage;
  848. ret = regulator_enable(data->reg);
  849. if (ret != 0) {
  850. dev_err(&pdev->dev,
  851. "failed to enable regulator: %d\n", ret);
  852. return ret;
  853. }
  854. /*
  855. * Setup a notifier block to update this if another device
  856. * causes the voltage to change
  857. */
  858. data->nb.notifier_call = &sht15_invalidate_voltage;
  859. ret = regulator_register_notifier(data->reg, &data->nb);
  860. if (ret) {
  861. dev_err(&pdev->dev,
  862. "regulator notifier request failed\n");
  863. regulator_disable(data->reg);
  864. return ret;
  865. }
  866. }
  867. /* Try requesting the GPIOs */
  868. data->sck = devm_gpiod_get(&pdev->dev, "clk", GPIOD_OUT_LOW);
  869. if (IS_ERR(data->sck)) {
  870. ret = PTR_ERR(data->sck);
  871. dev_err(&pdev->dev, "clock line GPIO request failed\n");
  872. goto err_release_reg;
  873. }
  874. data->data = devm_gpiod_get(&pdev->dev, "data", GPIOD_IN);
  875. if (IS_ERR(data->data)) {
  876. ret = PTR_ERR(data->data);
  877. dev_err(&pdev->dev, "data line GPIO request failed\n");
  878. goto err_release_reg;
  879. }
  880. ret = devm_request_irq(&pdev->dev, gpiod_to_irq(data->data),
  881. sht15_interrupt_fired,
  882. IRQF_TRIGGER_FALLING,
  883. "sht15 data",
  884. data);
  885. if (ret) {
  886. dev_err(&pdev->dev, "failed to get irq for data line\n");
  887. goto err_release_reg;
  888. }
  889. disable_irq_nosync(gpiod_to_irq(data->data));
  890. ret = sht15_connection_reset(data);
  891. if (ret)
  892. goto err_release_reg;
  893. ret = sht15_soft_reset(data);
  894. if (ret)
  895. goto err_release_reg;
  896. ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
  897. if (ret) {
  898. dev_err(&pdev->dev, "sysfs create failed\n");
  899. goto err_release_reg;
  900. }
  901. data->hwmon_dev = hwmon_device_register(data->dev);
  902. if (IS_ERR(data->hwmon_dev)) {
  903. ret = PTR_ERR(data->hwmon_dev);
  904. goto err_release_sysfs_group;
  905. }
  906. return 0;
  907. err_release_sysfs_group:
  908. sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
  909. err_release_reg:
  910. if (!IS_ERR(data->reg)) {
  911. regulator_unregister_notifier(data->reg, &data->nb);
  912. regulator_disable(data->reg);
  913. }
  914. return ret;
  915. }
  916. static void sht15_remove(struct platform_device *pdev)
  917. {
  918. struct sht15_data *data = platform_get_drvdata(pdev);
  919. int ret;
  920. hwmon_device_unregister(data->hwmon_dev);
  921. sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
  922. ret = sht15_soft_reset(data);
  923. if (ret)
  924. dev_err(&pdev->dev, "Failed to reset device (%pe)\n", ERR_PTR(ret));
  925. if (!IS_ERR(data->reg)) {
  926. regulator_unregister_notifier(data->reg, &data->nb);
  927. regulator_disable(data->reg);
  928. }
  929. }
  930. static const struct platform_device_id sht15_device_ids[] = {
  931. { "sht10", sht10 },
  932. { "sht11", sht11 },
  933. { "sht15", sht15 },
  934. { "sht71", sht71 },
  935. { "sht75", sht75 },
  936. { }
  937. };
  938. MODULE_DEVICE_TABLE(platform, sht15_device_ids);
  939. static struct platform_driver sht15_driver = {
  940. .driver = {
  941. .name = "sht15",
  942. .of_match_table = of_match_ptr(sht15_dt_match),
  943. },
  944. .probe = sht15_probe,
  945. .remove = sht15_remove,
  946. .id_table = sht15_device_ids,
  947. };
  948. module_platform_driver(sht15_driver);
  949. MODULE_LICENSE("GPL");
  950. MODULE_DESCRIPTION("Sensirion SHT15 temperature and humidity sensor driver");