sht15.c 18 KB

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