sht15.c 20 KB

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