sht15.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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 <asm/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 read 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;
  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. ret = -EINVAL;
  815. dev_err(&pdev->dev, "no platform data supplied\n");
  816. goto err_free_data;
  817. }
  818. data->pdata = pdev->dev.platform_data;
  819. data->supply_uV = data->pdata->supply_mv * 1000;
  820. if (data->pdata->checksum)
  821. data->checksumming = true;
  822. if (data->pdata->no_otp_reload)
  823. status |= SHT15_STATUS_NO_OTP_RELOAD;
  824. if (data->pdata->low_resolution)
  825. status |= SHT15_STATUS_LOW_RESOLUTION;
  826. /*
  827. * If a regulator is available,
  828. * query what the supply voltage actually is!
  829. */
  830. data->reg = regulator_get(data->dev, "vcc");
  831. if (!IS_ERR(data->reg)) {
  832. int voltage;
  833. voltage = regulator_get_voltage(data->reg);
  834. if (voltage)
  835. data->supply_uV = voltage;
  836. regulator_enable(data->reg);
  837. /*
  838. * Setup a notifier block to update this if another device
  839. * causes the voltage to change
  840. */
  841. data->nb.notifier_call = &sht15_invalidate_voltage;
  842. ret = regulator_register_notifier(data->reg, &data->nb);
  843. if (ret) {
  844. dev_err(&pdev->dev,
  845. "regulator notifier request failed\n");
  846. regulator_disable(data->reg);
  847. regulator_put(data->reg);
  848. goto err_free_data;
  849. }
  850. }
  851. /* Try requesting the GPIOs */
  852. ret = gpio_request(data->pdata->gpio_sck, "SHT15 sck");
  853. if (ret) {
  854. dev_err(&pdev->dev, "gpio request failed\n");
  855. goto err_release_reg;
  856. }
  857. gpio_direction_output(data->pdata->gpio_sck, 0);
  858. ret = gpio_request(data->pdata->gpio_data, "SHT15 data");
  859. if (ret) {
  860. dev_err(&pdev->dev, "gpio request failed\n");
  861. goto err_release_gpio_sck;
  862. }
  863. ret = request_irq(gpio_to_irq(data->pdata->gpio_data),
  864. sht15_interrupt_fired,
  865. IRQF_TRIGGER_FALLING,
  866. "sht15 data",
  867. data);
  868. if (ret) {
  869. dev_err(&pdev->dev, "failed to get irq for data line\n");
  870. goto err_release_gpio_data;
  871. }
  872. disable_irq_nosync(gpio_to_irq(data->pdata->gpio_data));
  873. sht15_connection_reset(data);
  874. ret = sht15_soft_reset(data);
  875. if (ret)
  876. goto err_release_irq;
  877. /* write status with platform data options */
  878. if (status) {
  879. ret = sht15_send_status(data, status);
  880. if (ret)
  881. goto err_release_irq;
  882. }
  883. ret = sysfs_create_group(&pdev->dev.kobj, &sht15_attr_group);
  884. if (ret) {
  885. dev_err(&pdev->dev, "sysfs create failed\n");
  886. goto err_release_irq;
  887. }
  888. data->hwmon_dev = hwmon_device_register(data->dev);
  889. if (IS_ERR(data->hwmon_dev)) {
  890. ret = PTR_ERR(data->hwmon_dev);
  891. goto err_release_sysfs_group;
  892. }
  893. return 0;
  894. err_release_sysfs_group:
  895. sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
  896. err_release_irq:
  897. free_irq(gpio_to_irq(data->pdata->gpio_data), data);
  898. err_release_gpio_data:
  899. gpio_free(data->pdata->gpio_data);
  900. err_release_gpio_sck:
  901. gpio_free(data->pdata->gpio_sck);
  902. err_release_reg:
  903. if (!IS_ERR(data->reg)) {
  904. regulator_unregister_notifier(data->reg, &data->nb);
  905. regulator_disable(data->reg);
  906. regulator_put(data->reg);
  907. }
  908. err_free_data:
  909. kfree(data);
  910. error_ret:
  911. return ret;
  912. }
  913. static int __devexit sht15_remove(struct platform_device *pdev)
  914. {
  915. struct sht15_data *data = platform_get_drvdata(pdev);
  916. /*
  917. * Make sure any reads from the device are done and
  918. * prevent new ones beginning
  919. */
  920. mutex_lock(&data->read_lock);
  921. if (sht15_soft_reset(data)) {
  922. mutex_unlock(&data->read_lock);
  923. return -EFAULT;
  924. }
  925. hwmon_device_unregister(data->hwmon_dev);
  926. sysfs_remove_group(&pdev->dev.kobj, &sht15_attr_group);
  927. if (!IS_ERR(data->reg)) {
  928. regulator_unregister_notifier(data->reg, &data->nb);
  929. regulator_disable(data->reg);
  930. regulator_put(data->reg);
  931. }
  932. free_irq(gpio_to_irq(data->pdata->gpio_data), data);
  933. gpio_free(data->pdata->gpio_data);
  934. gpio_free(data->pdata->gpio_sck);
  935. mutex_unlock(&data->read_lock);
  936. kfree(data);
  937. return 0;
  938. }
  939. /*
  940. * sht_drivers simultaneously refers to __devinit and __devexit function
  941. * which causes spurious section mismatch warning. So use __refdata to
  942. * get rid from this.
  943. */
  944. static struct platform_driver __refdata sht_drivers[] = {
  945. {
  946. .driver = {
  947. .name = "sht10",
  948. .owner = THIS_MODULE,
  949. },
  950. .probe = sht15_probe,
  951. .remove = __devexit_p(sht15_remove),
  952. }, {
  953. .driver = {
  954. .name = "sht11",
  955. .owner = THIS_MODULE,
  956. },
  957. .probe = sht15_probe,
  958. .remove = __devexit_p(sht15_remove),
  959. }, {
  960. .driver = {
  961. .name = "sht15",
  962. .owner = THIS_MODULE,
  963. },
  964. .probe = sht15_probe,
  965. .remove = __devexit_p(sht15_remove),
  966. }, {
  967. .driver = {
  968. .name = "sht71",
  969. .owner = THIS_MODULE,
  970. },
  971. .probe = sht15_probe,
  972. .remove = __devexit_p(sht15_remove),
  973. }, {
  974. .driver = {
  975. .name = "sht75",
  976. .owner = THIS_MODULE,
  977. },
  978. .probe = sht15_probe,
  979. .remove = __devexit_p(sht15_remove),
  980. },
  981. };
  982. static int __init sht15_init(void)
  983. {
  984. int ret;
  985. int i;
  986. for (i = 0; i < ARRAY_SIZE(sht_drivers); i++) {
  987. ret = platform_driver_register(&sht_drivers[i]);
  988. if (ret)
  989. goto error_unreg;
  990. }
  991. return 0;
  992. error_unreg:
  993. while (--i >= 0)
  994. platform_driver_unregister(&sht_drivers[i]);
  995. return ret;
  996. }
  997. module_init(sht15_init);
  998. static void __exit sht15_exit(void)
  999. {
  1000. int i;
  1001. for (i = ARRAY_SIZE(sht_drivers) - 1; i >= 0; i--)
  1002. platform_driver_unregister(&sht_drivers[i]);
  1003. }
  1004. module_exit(sht15_exit);
  1005. MODULE_LICENSE("GPL");