sht15.c 28 KB

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