sht15.c 18 KB

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