sht15.c 25 KB

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