sht15.c 29 KB

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