sht15.c 28 KB

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