sht15.c 19 KB

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