sht15.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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 int sht15_connection_reset(struct sht15_data *data)
  200. {
  201. int i, err;
  202. err = gpio_direction_output(data->pdata->gpio_data, 1);
  203. if (err)
  204. return err;
  205. ndelay(SHT15_TSCKL);
  206. gpio_set_value(data->pdata->gpio_sck, 0);
  207. ndelay(SHT15_TSCKL);
  208. for (i = 0; i < 9; ++i) {
  209. gpio_set_value(data->pdata->gpio_sck, 1);
  210. ndelay(SHT15_TSCKH);
  211. gpio_set_value(data->pdata->gpio_sck, 0);
  212. ndelay(SHT15_TSCKL);
  213. }
  214. return 0;
  215. }
  216. /**
  217. * sht15_send_bit() - send an individual bit to the device
  218. * @data: device state data
  219. * @val: value of bit to be sent
  220. */
  221. static inline void sht15_send_bit(struct sht15_data *data, int val)
  222. {
  223. gpio_set_value(data->pdata->gpio_data, val);
  224. ndelay(SHT15_TSU);
  225. gpio_set_value(data->pdata->gpio_sck, 1);
  226. ndelay(SHT15_TSCKH);
  227. gpio_set_value(data->pdata->gpio_sck, 0);
  228. ndelay(SHT15_TSCKL); /* clock low time */
  229. }
  230. /**
  231. * sht15_transmission_start() - specific sequence for new transmission
  232. * @data: device state data
  233. *
  234. * Timings for this are not documented on the data sheet, so very
  235. * conservative ones used in implementation. This implements
  236. * figure 12 on the data sheet.
  237. */
  238. static int sht15_transmission_start(struct sht15_data *data)
  239. {
  240. int err;
  241. /* ensure data is high and output */
  242. err = gpio_direction_output(data->pdata->gpio_data, 1);
  243. if (err)
  244. return err;
  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, 0);
  251. ndelay(SHT15_TSU);
  252. gpio_set_value(data->pdata->gpio_sck, 0);
  253. ndelay(SHT15_TSCKL);
  254. gpio_set_value(data->pdata->gpio_sck, 1);
  255. ndelay(SHT15_TSCKH);
  256. gpio_set_value(data->pdata->gpio_data, 1);
  257. ndelay(SHT15_TSU);
  258. gpio_set_value(data->pdata->gpio_sck, 0);
  259. ndelay(SHT15_TSCKL);
  260. return 0;
  261. }
  262. /**
  263. * sht15_send_byte() - send a single byte to the device
  264. * @data: device state
  265. * @byte: value to be sent
  266. */
  267. static void sht15_send_byte(struct sht15_data *data, u8 byte)
  268. {
  269. int i;
  270. for (i = 0; i < 8; i++) {
  271. sht15_send_bit(data, !!(byte & 0x80));
  272. byte <<= 1;
  273. }
  274. }
  275. /**
  276. * sht15_wait_for_response() - checks for ack from device
  277. * @data: device state
  278. */
  279. static int sht15_wait_for_response(struct sht15_data *data)
  280. {
  281. int err;
  282. err = gpio_direction_input(data->pdata->gpio_data);
  283. if (err)
  284. return err;
  285. gpio_set_value(data->pdata->gpio_sck, 1);
  286. ndelay(SHT15_TSCKH);
  287. if (gpio_get_value(data->pdata->gpio_data)) {
  288. gpio_set_value(data->pdata->gpio_sck, 0);
  289. dev_err(data->dev, "Command not acknowledged\n");
  290. err = sht15_connection_reset(data);
  291. if (err)
  292. return err;
  293. return -EIO;
  294. }
  295. gpio_set_value(data->pdata->gpio_sck, 0);
  296. ndelay(SHT15_TSCKL);
  297. return 0;
  298. }
  299. /**
  300. * sht15_send_cmd() - Sends a command to the device.
  301. * @data: device state
  302. * @cmd: command byte to be sent
  303. *
  304. * On entry, sck is output low, data is output pull high
  305. * and the interrupt disabled.
  306. */
  307. static int sht15_send_cmd(struct sht15_data *data, u8 cmd)
  308. {
  309. int err;
  310. err = sht15_transmission_start(data);
  311. if (err)
  312. return err;
  313. sht15_send_byte(data, cmd);
  314. return sht15_wait_for_response(data);
  315. }
  316. /**
  317. * sht15_soft_reset() - send a soft reset command
  318. * @data: sht15 specific data.
  319. *
  320. * As described in section 3.2 of the datasheet.
  321. */
  322. static int sht15_soft_reset(struct sht15_data *data)
  323. {
  324. int ret;
  325. ret = sht15_send_cmd(data, SHT15_SOFT_RESET);
  326. if (ret)
  327. return ret;
  328. msleep(SHT15_TSRST);
  329. /* device resets default hardware status register value */
  330. data->val_status = 0;
  331. return ret;
  332. }
  333. /**
  334. * sht15_ack() - send a ack
  335. * @data: sht15 specific data.
  336. *
  337. * Each byte of data is acknowledged by pulling the data line
  338. * low for one clock pulse.
  339. */
  340. static int sht15_ack(struct sht15_data *data)
  341. {
  342. int err;
  343. err = gpio_direction_output(data->pdata->gpio_data, 0);
  344. if (err)
  345. return err;
  346. ndelay(SHT15_TSU);
  347. gpio_set_value(data->pdata->gpio_sck, 1);
  348. ndelay(SHT15_TSU);
  349. gpio_set_value(data->pdata->gpio_sck, 0);
  350. ndelay(SHT15_TSU);
  351. gpio_set_value(data->pdata->gpio_data, 1);
  352. return gpio_direction_input(data->pdata->gpio_data);
  353. }
  354. /**
  355. * sht15_end_transmission() - notify device of end of transmission
  356. * @data: device state.
  357. *
  358. * This is basically a NAK (single clock pulse, data high).
  359. */
  360. static int sht15_end_transmission(struct sht15_data *data)
  361. {
  362. int err;
  363. err = gpio_direction_output(data->pdata->gpio_data, 1);
  364. if (err)
  365. return err;
  366. ndelay(SHT15_TSU);
  367. gpio_set_value(data->pdata->gpio_sck, 1);
  368. ndelay(SHT15_TSCKH);
  369. gpio_set_value(data->pdata->gpio_sck, 0);
  370. ndelay(SHT15_TSCKL);
  371. return 0;
  372. }
  373. /**
  374. * sht15_read_byte() - Read a byte back from the device
  375. * @data: device state.
  376. */
  377. static u8 sht15_read_byte(struct sht15_data *data)
  378. {
  379. int i;
  380. u8 byte = 0;
  381. for (i = 0; i < 8; ++i) {
  382. byte <<= 1;
  383. gpio_set_value(data->pdata->gpio_sck, 1);
  384. ndelay(SHT15_TSCKH);
  385. byte |= !!gpio_get_value(data->pdata->gpio_data);
  386. gpio_set_value(data->pdata->gpio_sck, 0);
  387. ndelay(SHT15_TSCKL);
  388. }
  389. return byte;
  390. }
  391. /**
  392. * sht15_send_status() - write the status register byte
  393. * @data: sht15 specific data.
  394. * @status: the byte to set the status register with.
  395. *
  396. * As described in figure 14 and table 5 of the datasheet.
  397. */
  398. static int sht15_send_status(struct sht15_data *data, u8 status)
  399. {
  400. int err;
  401. err = sht15_send_cmd(data, SHT15_WRITE_STATUS);
  402. if (err)
  403. return err;
  404. err = gpio_direction_output(data->pdata->gpio_data, 1);
  405. if (err)
  406. return err;
  407. ndelay(SHT15_TSU);
  408. sht15_send_byte(data, status);
  409. err = sht15_wait_for_response(data);
  410. if (err)
  411. return err;
  412. data->val_status = status;
  413. return 0;
  414. }
  415. /**
  416. * sht15_update_status() - get updated status register from device if too old
  417. * @data: device instance specific data.
  418. *
  419. * As described in figure 15 and table 5 of the datasheet.
  420. */
  421. static int sht15_update_status(struct sht15_data *data)
  422. {
  423. int ret = 0;
  424. u8 status;
  425. u8 previous_config;
  426. u8 dev_checksum = 0;
  427. u8 checksum_vals[2];
  428. int timeout = HZ;
  429. mutex_lock(&data->read_lock);
  430. if (time_after(jiffies, data->last_status + timeout)
  431. || !data->status_valid) {
  432. ret = sht15_send_cmd(data, SHT15_READ_STATUS);
  433. if (ret)
  434. goto unlock;
  435. status = sht15_read_byte(data);
  436. if (data->checksumming) {
  437. sht15_ack(data);
  438. dev_checksum = sht15_reverse(sht15_read_byte(data));
  439. checksum_vals[0] = SHT15_READ_STATUS;
  440. checksum_vals[1] = status;
  441. data->checksum_ok = (sht15_crc8(data, checksum_vals, 2)
  442. == dev_checksum);
  443. }
  444. ret = sht15_end_transmission(data);
  445. if (ret)
  446. goto unlock;
  447. /*
  448. * Perform checksum validation on the received data.
  449. * Specification mentions that in case a checksum verification
  450. * fails, a soft reset command must be sent to the device.
  451. */
  452. if (data->checksumming && !data->checksum_ok) {
  453. previous_config = data->val_status & 0x07;
  454. ret = sht15_soft_reset(data);
  455. if (ret)
  456. goto unlock;
  457. if (previous_config) {
  458. ret = sht15_send_status(data, previous_config);
  459. if (ret) {
  460. dev_err(data->dev,
  461. "CRC validation failed, unable "
  462. "to restore device settings\n");
  463. goto unlock;
  464. }
  465. }
  466. ret = -EAGAIN;
  467. goto unlock;
  468. }
  469. data->val_status = status;
  470. data->status_valid = true;
  471. data->last_status = jiffies;
  472. }
  473. unlock:
  474. mutex_unlock(&data->read_lock);
  475. return ret;
  476. }
  477. /**
  478. * sht15_measurement() - get a new value from device
  479. * @data: device instance specific data
  480. * @command: command sent to request value
  481. * @timeout_msecs: timeout after which comms are assumed
  482. * to have failed are reset.
  483. */
  484. static int sht15_measurement(struct sht15_data *data,
  485. int command,
  486. int timeout_msecs)
  487. {
  488. int ret;
  489. u8 previous_config;
  490. ret = sht15_send_cmd(data, command);
  491. if (ret)
  492. return ret;
  493. ret = gpio_direction_input(data->pdata->gpio_data);
  494. if (ret)
  495. return ret;
  496. atomic_set(&data->interrupt_handled, 0);
  497. enable_irq(gpio_to_irq(data->pdata->gpio_data));
  498. if (gpio_get_value(data->pdata->gpio_data) == 0) {
  499. disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
  500. /* Only relevant if the interrupt hasn't occurred. */
  501. if (!atomic_read(&data->interrupt_handled))
  502. schedule_work(&data->read_work);
  503. }
  504. ret = wait_event_timeout(data->wait_queue,
  505. (data->state == SHT15_READING_NOTHING),
  506. msecs_to_jiffies(timeout_msecs));
  507. if (data->state != SHT15_READING_NOTHING) { /* I/O error occurred */
  508. data->state = SHT15_READING_NOTHING;
  509. return -EIO;
  510. } else if (ret == 0) { /* timeout occurred */
  511. disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
  512. ret = sht15_connection_reset(data);
  513. if (ret)
  514. return ret;
  515. return -ETIME;
  516. }
  517. /*
  518. * Perform checksum validation on the received data.
  519. * Specification mentions that in case a checksum verification fails,
  520. * a soft reset command must be sent to the device.
  521. */
  522. if (data->checksumming && !data->checksum_ok) {
  523. previous_config = data->val_status & 0x07;
  524. ret = sht15_soft_reset(data);
  525. if (ret)
  526. return ret;
  527. if (previous_config) {
  528. ret = sht15_send_status(data, previous_config);
  529. if (ret) {
  530. dev_err(data->dev,
  531. "CRC validation failed, unable "
  532. "to restore device settings\n");
  533. return ret;
  534. }
  535. }
  536. return -EAGAIN;
  537. }
  538. return 0;
  539. }
  540. /**
  541. * sht15_update_measurements() - get updated measures from device if too old
  542. * @data: device state
  543. */
  544. static int sht15_update_measurements(struct sht15_data *data)
  545. {
  546. int ret = 0;
  547. int timeout = HZ;
  548. mutex_lock(&data->read_lock);
  549. if (time_after(jiffies, data->last_measurement + timeout)
  550. || !data->measurements_valid) {
  551. data->state = SHT15_READING_HUMID;
  552. ret = sht15_measurement(data, SHT15_MEASURE_RH, 160);
  553. if (ret)
  554. goto unlock;
  555. data->state = SHT15_READING_TEMP;
  556. ret = sht15_measurement(data, SHT15_MEASURE_TEMP, 400);
  557. if (ret)
  558. goto unlock;
  559. data->measurements_valid = true;
  560. data->last_measurement = jiffies;
  561. }
  562. unlock:
  563. mutex_unlock(&data->read_lock);
  564. return ret;
  565. }
  566. /**
  567. * sht15_calc_temp() - convert the raw reading to a temperature
  568. * @data: device state
  569. *
  570. * As per section 4.3 of the data sheet.
  571. */
  572. static inline int sht15_calc_temp(struct sht15_data *data)
  573. {
  574. int d1 = temppoints[0].d1;
  575. int d2 = (data->val_status & SHT15_STATUS_LOW_RESOLUTION) ? 40 : 10;
  576. int i;
  577. for (i = ARRAY_SIZE(temppoints) - 1; i > 0; i--)
  578. /* Find pointer to interpolate */
  579. if (data->supply_uv > temppoints[i - 1].vdd) {
  580. d1 = (data->supply_uv - temppoints[i - 1].vdd)
  581. * (temppoints[i].d1 - temppoints[i - 1].d1)
  582. / (temppoints[i].vdd - temppoints[i - 1].vdd)
  583. + temppoints[i - 1].d1;
  584. break;
  585. }
  586. return data->val_temp * d2 + d1;
  587. }
  588. /**
  589. * sht15_calc_humid() - using last temperature convert raw to humid
  590. * @data: device state
  591. *
  592. * This is the temperature compensated version as per section 4.2 of
  593. * the data sheet.
  594. *
  595. * The sensor is assumed to be V3, which is compatible with V4.
  596. * Humidity conversion coefficients are shown in table 7 of the datasheet.
  597. */
  598. static inline int sht15_calc_humid(struct sht15_data *data)
  599. {
  600. int rh_linear; /* milli percent */
  601. int temp = sht15_calc_temp(data);
  602. int c2, c3;
  603. int t2;
  604. const int c1 = -4;
  605. if (data->val_status & SHT15_STATUS_LOW_RESOLUTION) {
  606. c2 = 648000; /* x 10 ^ -6 */
  607. c3 = -7200; /* x 10 ^ -7 */
  608. t2 = 1280;
  609. } else {
  610. c2 = 40500; /* x 10 ^ -6 */
  611. c3 = -28; /* x 10 ^ -7 */
  612. t2 = 80;
  613. }
  614. rh_linear = c1 * 1000
  615. + c2 * data->val_humid / 1000
  616. + (data->val_humid * data->val_humid * c3) / 10000;
  617. return (temp - 25000) * (10000 + t2 * data->val_humid)
  618. / 1000000 + rh_linear;
  619. }
  620. /**
  621. * sht15_show_status() - show status information in sysfs
  622. * @dev: device.
  623. * @attr: device attribute.
  624. * @buf: sysfs buffer where information is written to.
  625. *
  626. * Will be called on read access to temp1_fault, humidity1_fault
  627. * and heater_enable sysfs attributes.
  628. * Returns number of bytes written into buffer, negative errno on error.
  629. */
  630. static ssize_t sht15_show_status(struct device *dev,
  631. struct device_attribute *attr,
  632. char *buf)
  633. {
  634. int ret;
  635. struct sht15_data *data = dev_get_drvdata(dev);
  636. u8 bit = to_sensor_dev_attr(attr)->index;
  637. ret = sht15_update_status(data);
  638. return ret ? ret : sprintf(buf, "%d\n", !!(data->val_status & bit));
  639. }
  640. /**
  641. * sht15_store_heater() - change heater state via sysfs
  642. * @dev: device.
  643. * @attr: device attribute.
  644. * @buf: sysfs buffer to read the new heater state from.
  645. * @count: length of the data.
  646. *
  647. * Will be called on write access to heater_enable sysfs attribute.
  648. * Returns number of bytes actually decoded, negative errno on error.
  649. */
  650. static ssize_t sht15_store_heater(struct device *dev,
  651. struct device_attribute *attr,
  652. const char *buf, size_t count)
  653. {
  654. int ret;
  655. struct sht15_data *data = dev_get_drvdata(dev);
  656. long value;
  657. u8 status;
  658. if (kstrtol(buf, 10, &value))
  659. return -EINVAL;
  660. mutex_lock(&data->read_lock);
  661. status = data->val_status & 0x07;
  662. if (!!value)
  663. status |= SHT15_STATUS_HEATER;
  664. else
  665. status &= ~SHT15_STATUS_HEATER;
  666. ret = sht15_send_status(data, status);
  667. mutex_unlock(&data->read_lock);
  668. return ret ? ret : count;
  669. }
  670. /**
  671. * sht15_show_temp() - show temperature measurement value in sysfs
  672. * @dev: device.
  673. * @attr: device attribute.
  674. * @buf: sysfs buffer where measurement values are written to.
  675. *
  676. * Will be called on read access to temp1_input sysfs attribute.
  677. * Returns number of bytes written into buffer, negative errno on error.
  678. */
  679. static ssize_t sht15_show_temp(struct device *dev,
  680. struct device_attribute *attr,
  681. char *buf)
  682. {
  683. int ret;
  684. struct sht15_data *data = dev_get_drvdata(dev);
  685. /* Technically no need to read humidity as well */
  686. ret = sht15_update_measurements(data);
  687. return ret ? ret : sprintf(buf, "%d\n",
  688. sht15_calc_temp(data));
  689. }
  690. /**
  691. * sht15_show_humidity() - show humidity measurement value in sysfs
  692. * @dev: device.
  693. * @attr: device attribute.
  694. * @buf: sysfs buffer where measurement values are written to.
  695. *
  696. * Will be called on read access to humidity1_input sysfs attribute.
  697. * Returns number of bytes written into buffer, negative errno on error.
  698. */
  699. static ssize_t sht15_show_humidity(struct device *dev,
  700. struct device_attribute *attr,
  701. char *buf)
  702. {
  703. int ret;
  704. struct sht15_data *data = dev_get_drvdata(dev);
  705. ret = sht15_update_measurements(data);
  706. return ret ? ret : sprintf(buf, "%d\n", sht15_calc_humid(data));
  707. }
  708. static ssize_t show_name(struct device *dev,
  709. struct device_attribute *attr,
  710. char *buf)
  711. {
  712. struct platform_device *pdev = to_platform_device(dev);
  713. return sprintf(buf, "%s\n", pdev->name);
  714. }
  715. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO,
  716. sht15_show_temp, NULL, 0);
  717. static SENSOR_DEVICE_ATTR(humidity1_input, S_IRUGO,
  718. sht15_show_humidity, NULL, 0);
  719. static SENSOR_DEVICE_ATTR(temp1_fault, S_IRUGO, sht15_show_status, NULL,
  720. SHT15_STATUS_LOW_BATTERY);
  721. static SENSOR_DEVICE_ATTR(humidity1_fault, S_IRUGO, sht15_show_status, NULL,
  722. SHT15_STATUS_LOW_BATTERY);
  723. static SENSOR_DEVICE_ATTR(heater_enable, S_IRUGO | S_IWUSR, sht15_show_status,
  724. sht15_store_heater, SHT15_STATUS_HEATER);
  725. static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
  726. static struct attribute *sht15_attrs[] = {
  727. &sensor_dev_attr_temp1_input.dev_attr.attr,
  728. &sensor_dev_attr_humidity1_input.dev_attr.attr,
  729. &sensor_dev_attr_temp1_fault.dev_attr.attr,
  730. &sensor_dev_attr_humidity1_fault.dev_attr.attr,
  731. &sensor_dev_attr_heater_enable.dev_attr.attr,
  732. &dev_attr_name.attr,
  733. NULL,
  734. };
  735. static const struct attribute_group sht15_attr_group = {
  736. .attrs = sht15_attrs,
  737. };
  738. static irqreturn_t sht15_interrupt_fired(int irq, void *d)
  739. {
  740. struct sht15_data *data = d;
  741. /* First disable the interrupt */
  742. disable_irq_nosync(irq);
  743. atomic_inc(&data->interrupt_handled);
  744. /* Then schedule a reading work struct */
  745. if (data->state != SHT15_READING_NOTHING)
  746. schedule_work(&data->read_work);
  747. return IRQ_HANDLED;
  748. }
  749. static void sht15_bh_read_data(struct work_struct *work_s)
  750. {
  751. uint16_t val = 0;
  752. u8 dev_checksum = 0;
  753. u8 checksum_vals[3];
  754. struct sht15_data *data
  755. = container_of(work_s, struct sht15_data,
  756. read_work);
  757. /* Firstly, verify the line is low */
  758. if (gpio_get_value(data->pdata->gpio_data)) {
  759. /*
  760. * If not, then start the interrupt again - care here as could
  761. * have gone low in meantime so verify it hasn't!
  762. */
  763. atomic_set(&data->interrupt_handled, 0);
  764. enable_irq(gpio_to_irq(data->pdata->gpio_data));
  765. /* If still not occurred or another handler was scheduled */
  766. if (gpio_get_value(data->pdata->gpio_data)
  767. || atomic_read(&data->interrupt_handled))
  768. return;
  769. }
  770. /* Read the data back from the device */
  771. val = sht15_read_byte(data);
  772. val <<= 8;
  773. if (sht15_ack(data))
  774. goto wakeup;
  775. val |= sht15_read_byte(data);
  776. if (data->checksumming) {
  777. /*
  778. * Ask the device for a checksum and read it back.
  779. * Note: the device sends the checksum byte reversed.
  780. */
  781. if (sht15_ack(data))
  782. goto wakeup;
  783. dev_checksum = sht15_reverse(sht15_read_byte(data));
  784. checksum_vals[0] = (data->state == SHT15_READING_TEMP) ?
  785. SHT15_MEASURE_TEMP : SHT15_MEASURE_RH;
  786. checksum_vals[1] = (u8) (val >> 8);
  787. checksum_vals[2] = (u8) val;
  788. data->checksum_ok
  789. = (sht15_crc8(data, checksum_vals, 3) == dev_checksum);
  790. }
  791. /* Tell the device we are done */
  792. if (sht15_end_transmission(data))
  793. goto wakeup;
  794. switch (data->state) {
  795. case SHT15_READING_TEMP:
  796. data->val_temp = val;
  797. break;
  798. case SHT15_READING_HUMID:
  799. data->val_humid = val;
  800. break;
  801. default:
  802. break;
  803. }
  804. data->state = SHT15_READING_NOTHING;
  805. wakeup:
  806. wake_up(&data->wait_queue);
  807. }
  808. static void sht15_update_voltage(struct work_struct *work_s)
  809. {
  810. struct sht15_data *data
  811. = container_of(work_s, struct sht15_data,
  812. update_supply_work);
  813. data->supply_uv = regulator_get_voltage(data->reg);
  814. }
  815. /**
  816. * sht15_invalidate_voltage() - mark supply voltage invalid when notified by reg
  817. * @nb: associated notification structure
  818. * @event: voltage regulator state change event code
  819. * @ignored: function parameter - ignored here
  820. *
  821. * Note that as the notification code holds the regulator lock, we have
  822. * to schedule an update of the supply voltage rather than getting it directly.
  823. */
  824. static int sht15_invalidate_voltage(struct notifier_block *nb,
  825. unsigned long event,
  826. void *ignored)
  827. {
  828. struct sht15_data *data = container_of(nb, struct sht15_data, nb);
  829. if (event == REGULATOR_EVENT_VOLTAGE_CHANGE)
  830. data->supply_uv_valid = false;
  831. schedule_work(&data->update_supply_work);
  832. return NOTIFY_OK;
  833. }
  834. static int sht15_probe(struct platform_device *pdev)
  835. {
  836. int ret;
  837. struct sht15_data *data;
  838. u8 status = 0;
  839. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  840. if (!data)
  841. return -ENOMEM;
  842. INIT_WORK(&data->read_work, sht15_bh_read_data);
  843. INIT_WORK(&data->update_supply_work, sht15_update_voltage);
  844. platform_set_drvdata(pdev, data);
  845. mutex_init(&data->read_lock);
  846. data->dev = &pdev->dev;
  847. init_waitqueue_head(&data->wait_queue);
  848. if (dev_get_platdata(&pdev->dev) == NULL) {
  849. dev_err(&pdev->dev, "no platform data supplied\n");
  850. return -EINVAL;
  851. }
  852. data->pdata = dev_get_platdata(&pdev->dev);
  853. data->supply_uv = data->pdata->supply_mv * 1000;
  854. if (data->pdata->checksum)
  855. data->checksumming = true;
  856. if (data->pdata->no_otp_reload)
  857. status |= SHT15_STATUS_NO_OTP_RELOAD;
  858. if (data->pdata->low_resolution)
  859. status |= SHT15_STATUS_LOW_RESOLUTION;
  860. /*
  861. * If a regulator is available,
  862. * query what the supply voltage actually is!
  863. */
  864. data->reg = devm_regulator_get_optional(data->dev, "vcc");
  865. if (!IS_ERR(data->reg)) {
  866. int voltage;
  867. voltage = regulator_get_voltage(data->reg);
  868. if (voltage)
  869. data->supply_uv = voltage;
  870. ret = regulator_enable(data->reg);
  871. if (ret != 0) {
  872. dev_err(&pdev->dev,
  873. "failed to enable regulator: %d\n", ret);
  874. return ret;
  875. }
  876. /*
  877. * Setup a notifier block to update this if another device
  878. * causes the voltage to change
  879. */
  880. data->nb.notifier_call = &sht15_invalidate_voltage;
  881. ret = regulator_register_notifier(data->reg, &data->nb);
  882. if (ret) {
  883. dev_err(&pdev->dev,
  884. "regulator notifier request failed\n");
  885. regulator_disable(data->reg);
  886. return ret;
  887. }
  888. }
  889. /* Try requesting the GPIOs */
  890. ret = devm_gpio_request_one(&pdev->dev, data->pdata->gpio_sck,
  891. GPIOF_OUT_INIT_LOW, "SHT15 sck");
  892. if (ret) {
  893. dev_err(&pdev->dev, "clock line GPIO request failed\n");
  894. goto err_release_reg;
  895. }
  896. ret = devm_gpio_request(&pdev->dev, data->pdata->gpio_data,
  897. "SHT15 data");
  898. if (ret) {
  899. dev_err(&pdev->dev, "data line GPIO request failed\n");
  900. goto err_release_reg;
  901. }
  902. ret = devm_request_irq(&pdev->dev, gpio_to_irq(data->pdata->gpio_data),
  903. sht15_interrupt_fired,
  904. IRQF_TRIGGER_FALLING,
  905. "sht15 data",
  906. data);
  907. if (ret) {
  908. dev_err(&pdev->dev, "failed to get irq for data line\n");
  909. goto err_release_reg;
  910. }
  911. disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
  912. ret = sht15_connection_reset(data);
  913. if (ret)
  914. goto err_release_reg;
  915. ret = sht15_soft_reset(data);
  916. if (ret)
  917. goto err_release_reg;
  918. /* write status with platform data options */
  919. if (status) {
  920. ret = sht15_send_status(data, status);
  921. if (ret)
  922. goto err_release_reg;
  923. }
  924. ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
  925. if (ret) {
  926. dev_err(&pdev->dev, "sysfs create failed\n");
  927. goto err_release_reg;
  928. }
  929. data->hwmon_dev = hwmon_device_register(data->dev);
  930. if (IS_ERR(data->hwmon_dev)) {
  931. ret = PTR_ERR(data->hwmon_dev);
  932. goto err_release_sysfs_group;
  933. }
  934. return 0;
  935. err_release_sysfs_group:
  936. sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
  937. err_release_reg:
  938. if (!IS_ERR(data->reg)) {
  939. regulator_unregister_notifier(data->reg, &data->nb);
  940. regulator_disable(data->reg);
  941. }
  942. return ret;
  943. }
  944. static int sht15_remove(struct platform_device *pdev)
  945. {
  946. struct sht15_data *data = platform_get_drvdata(pdev);
  947. /*
  948. * Make sure any reads from the device are done and
  949. * prevent new ones beginning
  950. */
  951. mutex_lock(&data->read_lock);
  952. if (sht15_soft_reset(data)) {
  953. mutex_unlock(&data->read_lock);
  954. return -EFAULT;
  955. }
  956. hwmon_device_unregister(data->hwmon_dev);
  957. sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
  958. if (!IS_ERR(data->reg)) {
  959. regulator_unregister_notifier(data->reg, &data->nb);
  960. regulator_disable(data->reg);
  961. }
  962. mutex_unlock(&data->read_lock);
  963. return 0;
  964. }
  965. static const struct platform_device_id sht15_device_ids[] = {
  966. { "sht10", sht10 },
  967. { "sht11", sht11 },
  968. { "sht15", sht15 },
  969. { "sht71", sht71 },
  970. { "sht75", sht75 },
  971. { }
  972. };
  973. MODULE_DEVICE_TABLE(platform, sht15_device_ids);
  974. static struct platform_driver sht15_driver = {
  975. .driver = {
  976. .name = "sht15",
  977. },
  978. .probe = sht15_probe,
  979. .remove = sht15_remove,
  980. .id_table = sht15_device_ids,
  981. };
  982. module_platform_driver(sht15_driver);
  983. MODULE_LICENSE("GPL");
  984. MODULE_DESCRIPTION("Sensirion SHT15 temperature and humidity sensor driver");