sht15.c 30 KB

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