sht15.c 28 KB

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