25306d157698d078b1193e9ee4fcd0064e0900f0
[linux-3.10.git] / drivers / input / misc / pressure / bmp180.c
1 /* Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
2  *
3  * This software is licensed under the terms of the GNU General Public
4  * License version 2, as published by the Free Software Foundation, and
5  * may be copied, distributed, and modified under those terms.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  */
12
13 #include <linux/i2c.h>
14 #include <linux/input.h>
15 #include <linux/module.h>
16 #include <linux/miscdevice.h>
17 #include <linux/slab.h>
18 #include <linux/delay.h>
19 #include <linux/workqueue.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/mpu.h>
22
23
24 #define BMP180_NAME                     "bmp180"
25 #define BMP180_I2C_ADDR                 (0x77)
26 #define BMP180_HW_DELAY_MS              (10)
27 #define BMP180_POLL_DELAY_MS_DFLT       (200)
28 #define BMP180_MPU_RETRY_COUNT          (20)
29 #define BMP180_MPU_RETRY_DELAY_MS       (20)
30 #define BMP180_ERR_CNT_MAX              (20)
31 /* sampling delays */
32 #define BMP180_DELAY_ULP                (5)
33 #define BMP180_DELAY_ST                 (8)
34 #define BMP180_DELAY_HIGH_RES           (14)
35 #define BMP180_DELAY_UHIGH_RES          (26)
36 /* input poll values*/
37 #define BMP180_INPUT_RESOLUTION         (1)
38 #define BMP180_INPUT_DIVISOR            (100)
39 #define BMP180_INPUT_DELAY_MS_MIN       (BMP180_DELAY_UHIGH_RES)
40 #define BMP180_INPUT_POWER_UA           (12)
41 #define BMP180_PRESSURE_MIN             (30000)
42 #define BMP180_PRESSURE_MAX             (110000)
43 #define BMP180_PRESSURE_FUZZ            (5)
44 #define BMP180_PRESSURE_FLAT            (5)
45 /* BMP180 registers */
46 #define BMP180_REG_ID                   (0xD0)
47 #define BMP180_REG_ID_VAL               (0x55)
48 #define BMP180_REG_RESET                (0xE0)
49 #define BMP180_REG_RESET_VAL            (0xB6)
50 #define BMP180_REG_CTRL                 (0xF4)
51 #define BMP180_REG_CTRL_MODE_MASK       (0x1F)
52 #define BMP180_REG_CTRL_MODE_PRES       (0x34)
53 #define BMP180_REG_CTRL_MODE_TEMP       (0x2E)
54 #define BMP180_REG_CTRL_OSS             (6)
55 #define BMP180_REG_CTRL_SCO             (5)
56 #define BMP180_REG_OUT_MSB              (0xF6)
57 #define BMP180_REG_OUT_LSB              (0xF7)
58 #define BMP180_REG_OUT_XLSB             (0xF8)
59 /* ROM registers */
60 #define BMP180_REG_AC1                  (0xAA)
61 #define BMP180_REG_AC2                  (0xAC)
62 #define BMP180_REG_AC3                  (0xAE)
63 #define BMP180_REG_AC4                  (0xB0)
64 #define BMP180_REG_AC5                  (0xB2)
65 #define BMP180_REG_AC6                  (0xB4)
66 #define BMP180_REG_B1                   (0xB6)
67 #define BMP180_REG_B2                   (0xB8)
68 #define BMP180_REG_MB                   (0xBA)
69 #define BMP180_REG_MC                   (0xBC)
70 #define BMP180_REG_MD                   (0xBE)
71
72 #define WR                              (0)
73 #define RD                              (1)
74
75
76 /* regulator names in order of powering on */
77 static char *bmp180_vregs[] = {
78         "vdd",
79         "vddio",
80 };
81
82 static unsigned long bmp180_delay_ms_tbl[] = {
83         BMP180_DELAY_ULP,
84         BMP180_DELAY_ST,
85         BMP180_DELAY_HIGH_RES,
86         BMP180_DELAY_UHIGH_RES,
87 };
88
89
90 struct bmp180_rom {
91         s16 ac1;
92         s16 ac2;
93         s16 ac3;
94         u16 ac4;
95         u16 ac5;
96         u16 ac6;
97         s16 b1;
98         s16 b2;
99         s16 mb;
100         s16 mc;
101         s16 md;
102 };
103
104 struct bmp180_inf {
105         struct i2c_client *i2c;
106         struct input_dev *idev;
107         struct workqueue_struct *wq;
108         struct delayed_work dw;
109         struct regulator_bulk_data vreg[ARRAY_SIZE(bmp180_vregs)];
110         struct mpu_platform_data pdata;
111         struct bmp180_rom rom;          /* data for calibration */
112         unsigned long poll_delay_us;    /* requested sampling delay (us) */
113         unsigned int resolution;        /* report when new data outside this */
114         bool use_mpu;                   /* if device behind MPU */
115         bool initd;                     /* set if initialized */
116         bool enable;                    /* requested enable value */
117         bool report;                    /* used to report first valid sample */
118         bool port_en[2];                /* enable status of MPU write port */
119         int port_id[2];                 /* MPU port ID */
120         u8 data_out;                    /* write value to trigger a sample */
121         u8 range_index;                 /* oversampling value */
122         long UT;                        /* uncompensated temperature */
123         long UP;                        /* uncompensated pressure */
124         long temperature;               /* true temperature */
125         int pressure;                   /* true pressure hPa/100 Pa/1 mBar */
126 };
127
128
129 static int bmp180_i2c_rd(struct bmp180_inf *inf, u8 reg, u16 len, u8 *val)
130 {
131         struct i2c_msg msg[2];
132
133         msg[0].addr = BMP180_I2C_ADDR;
134         msg[0].flags = 0;
135         msg[0].len = 1;
136         msg[0].buf = &reg;
137         msg[1].addr = BMP180_I2C_ADDR;
138         msg[1].flags = I2C_M_RD;
139         msg[1].len = len;
140         msg[1].buf = val;
141         if (i2c_transfer(inf->i2c->adapter, msg, 2) != 2)
142                 return -EIO;
143
144         return 0;
145 }
146
147 static int bmp180_i2c_wr(struct bmp180_inf *inf, u8 reg, u8 val)
148 {
149         struct i2c_msg msg;
150         u8 buf[2];
151
152         buf[0] = reg;
153         buf[1] = val;
154         msg.addr = BMP180_I2C_ADDR;
155         msg.flags = 0;
156         msg.len = 2;
157         msg.buf = buf;
158         if (i2c_transfer(inf->i2c->adapter, &msg, 1) != 1)
159                 return -EIO;
160
161         return 0;
162 }
163
164 static int bmp180_vreg_dis(struct bmp180_inf *inf, int i)
165 {
166         int err = 0;
167
168         if (inf->vreg[i].ret && (inf->vreg[i].consumer != NULL)) {
169                 err = regulator_disable(inf->vreg[i].consumer);
170                 if (err)
171                         dev_err(&inf->i2c->dev, "%s %s ERR\n",
172                                 __func__, inf->vreg[i].supply);
173                 else
174                         dev_dbg(&inf->i2c->dev, "%s %s\n",
175                                 __func__, inf->vreg[i].supply);
176         }
177         inf->vreg[i].ret = 0;
178         return err;
179 }
180
181 static int bmp180_vreg_dis_all(struct bmp180_inf *inf)
182 {
183         unsigned int i;
184         int err = 0;
185
186         for (i = ARRAY_SIZE(bmp180_vregs); i > 0; i--)
187                 err |= bmp180_vreg_dis(inf, (i - 1));
188         return err;
189 }
190
191 static int bmp180_vreg_en(struct bmp180_inf *inf, int i)
192 {
193         int err = 0;
194
195         if ((!inf->vreg[i].ret) && (inf->vreg[i].consumer != NULL)) {
196                 err = regulator_enable(inf->vreg[i].consumer);
197                 if (!err) {
198                         inf->vreg[i].ret = 1;
199                         dev_dbg(&inf->i2c->dev, "%s %s\n",
200                                 __func__, inf->vreg[i].supply);
201                         err = 1; /* flag regulator state change */
202                 } else {
203                         dev_err(&inf->i2c->dev, "%s %s ERR\n",
204                                 __func__, inf->vreg[i].supply);
205                 }
206         }
207         return err;
208 }
209
210 static int bmp180_vreg_en_all(struct bmp180_inf *inf)
211 {
212         int i;
213         int err = 0;
214
215         for (i = 0; i < ARRAY_SIZE(bmp180_vregs); i++)
216                 err |= bmp180_vreg_en(inf, i);
217         return err;
218 }
219
220 static void bmp180_vreg_exit(struct bmp180_inf *inf)
221 {
222         int i;
223
224         for (i = 0; i < ARRAY_SIZE(bmp180_vregs); i++) {
225                 if (inf->vreg[i].consumer != NULL) {
226                         devm_regulator_put(inf->vreg[i].consumer);
227                         inf->vreg[i].consumer = NULL;
228                         dev_dbg(&inf->i2c->dev, "%s %s\n",
229                                 __func__, inf->vreg[i].supply);
230                 }
231         }
232 }
233
234 static int bmp180_vreg_init(struct bmp180_inf *inf)
235 {
236         unsigned int i;
237         int err = 0;
238
239         for (i = 0; i < ARRAY_SIZE(bmp180_vregs); i++) {
240                 inf->vreg[i].supply = bmp180_vregs[i];
241                 inf->vreg[i].ret = 0;
242                 inf->vreg[i].consumer = devm_regulator_get(&inf->i2c->dev,
243                                                           inf->vreg[i].supply);
244                 if (IS_ERR(inf->vreg[i].consumer)) {
245                         err |= PTR_ERR(inf->vreg[i].consumer);
246                         dev_err(&inf->i2c->dev, "%s err %d for %s\n",
247                                 __func__, err, inf->vreg[i].supply);
248                         inf->vreg[i].consumer = NULL;
249                 } else {
250                         dev_dbg(&inf->i2c->dev, "%s %s\n",
251                                 __func__, inf->vreg[i].supply);
252                 }
253         }
254         return err;
255 }
256
257 static int bmp180_pm(struct bmp180_inf *inf, bool enable)
258 {
259         int err;
260
261         if (enable) {
262                 err = bmp180_vreg_en_all(inf);
263                 if (err)
264                         mdelay(BMP180_HW_DELAY_MS);
265         } else {
266                 err = bmp180_vreg_dis_all(inf);
267         }
268         if (err > 0)
269                 err = 0;
270         if (err)
271                 dev_err(&inf->i2c->dev, "%s enable=%x ERR=%d\n",
272                         __func__, enable, err);
273         else
274                 dev_dbg(&inf->i2c->dev, "%s enable=%x\n",
275                         __func__, enable);
276         return err;
277 }
278
279 static int bmp180_port_free(struct bmp180_inf *inf, int port)
280 {
281         int err = 0;
282
283         if ((inf->use_mpu) && (inf->port_id[port] >= 0)) {
284                 err = nvi_mpu_port_free(inf->port_id[port]);
285                 if (!err)
286                         inf->port_id[port] = -1;
287         }
288         return err;
289 }
290
291 static int bmp180_ports_free(struct bmp180_inf *inf)
292 {
293         int err;
294
295         err = bmp180_port_free(inf, WR);
296         err |= bmp180_port_free(inf, RD);
297         return err;
298 }
299
300 static void bmp180_pm_exit(struct bmp180_inf *inf)
301 {
302         bmp180_ports_free(inf);
303         bmp180_pm(inf, false);
304         bmp180_vreg_exit(inf);
305 }
306
307 static int bmp180_pm_init(struct bmp180_inf *inf)
308 {
309         int err;
310
311         inf->initd = false;
312         inf->enable = false;
313         inf->port_en[WR] = false;
314         inf->port_en[RD] = false;
315         inf->port_id[WR] = -1;
316         inf->port_id[RD] = -1;
317         inf->resolution = 0;
318         inf->range_index = 0;
319         inf->poll_delay_us = (BMP180_POLL_DELAY_MS_DFLT * 1000);
320         bmp180_vreg_init(inf);
321         err = bmp180_pm(inf, true);
322         return err;
323 }
324
325 static int bmp180_nvi_mpu_bypass_request(struct bmp180_inf *inf)
326 {
327         int i;
328         int err = 0;
329
330         if (inf->use_mpu) {
331                 for (i = 0; i < BMP180_MPU_RETRY_COUNT; i++) {
332                         err = nvi_mpu_bypass_request(true);
333                         if ((!err) || (err == -EPERM))
334                                 break;
335
336                         mdelay(BMP180_MPU_RETRY_DELAY_MS);
337                 }
338                 if (err == -EPERM)
339                         err = 0;
340         }
341         return err;
342 }
343
344 static int bmp180_nvi_mpu_bypass_release(struct bmp180_inf *inf)
345 {
346         int err = 0;
347
348         if (inf->use_mpu)
349                 err = nvi_mpu_bypass_release();
350         return err;
351 }
352
353 static int bmp180_wr(struct bmp180_inf *inf, u8 reg, u8 val)
354 {
355         int err = 0;
356
357         err = bmp180_nvi_mpu_bypass_request(inf);
358         if (!err) {
359                 err = bmp180_i2c_wr(inf, reg, val);
360                 bmp180_nvi_mpu_bypass_release(inf);
361         }
362         return err;
363 }
364
365 static int bmp180_port_enable(struct bmp180_inf *inf, int port, bool enable)
366 {
367         int err = 0;
368
369         if (enable != inf->port_en[port]) {
370                 err = nvi_mpu_enable(inf->port_id[port], enable, false);
371                 if (!err)
372                         inf->port_en[port] = enable;
373         }
374         return err;
375 }
376
377 static int bmp180_ports_enable(struct bmp180_inf *inf, bool enable)
378 {
379         int err;
380
381         err = bmp180_port_enable(inf, WR, enable);
382         err |= bmp180_port_enable(inf, RD, enable);
383         return err;
384 }
385
386 static int bmp180_reset(struct bmp180_inf *inf)
387 {
388         int err = 0;
389
390         if (inf->use_mpu)
391                 err = bmp180_ports_enable(inf, false);
392         else
393                 cancel_delayed_work_sync(&inf->dw);
394         if (err)
395                 return err;
396
397         err = bmp180_wr(inf, BMP180_REG_RESET, BMP180_REG_RESET_VAL);
398         if (!err)
399                 mdelay(BMP180_HW_DELAY_MS);
400         if (inf->use_mpu) {
401                 err |= nvi_mpu_data_out(inf->port_id[WR],
402                                         BMP180_REG_CTRL_MODE_TEMP);
403                 err |= bmp180_ports_enable(inf, true);
404         } else {
405                 err = bmp180_wr(inf, BMP180_REG_CTRL,
406                                 BMP180_REG_CTRL_MODE_TEMP);
407                 queue_delayed_work(inf->wq, &inf->dw,
408                                    usecs_to_jiffies(inf->poll_delay_us));
409         }
410         return err;
411 }
412
413 static int bmp180_delay(struct bmp180_inf *inf, unsigned long delay_us)
414 {
415         int err = 0;
416
417         if (inf->use_mpu)
418                 err = nvi_mpu_delay_us(inf->port_id[RD], delay_us);
419         return err;
420 }
421
422 static int bmp180_init_hw(struct bmp180_inf *inf)
423 {
424         u8 *p_rom1;
425         u8 *p_rom2;
426         u8 tmp;
427         int i;
428         int err = 0;
429
430         inf->UT = 0;
431         inf->UP = 0;
432         inf->temperature = 0;
433         inf->pressure = 0;
434         p_rom1 = (u8 *)&inf->rom.ac1;
435         err = bmp180_nvi_mpu_bypass_request(inf);
436         if (!err) {
437                 err = bmp180_i2c_rd(inf, BMP180_REG_AC1, 22, p_rom1);
438                 bmp180_nvi_mpu_bypass_release(inf);
439         }
440         if (err)
441                 return err;
442
443         for (i = 0; i < 11; i++) {
444                 p_rom2 = p_rom1;
445                 tmp = *p_rom1;
446                 *p_rom1++ = *++p_rom2;
447                 *p_rom2 = tmp;
448                 p_rom1++;
449         }
450         inf->initd = true;
451         return err;
452 }
453
454 static void bmp180_calc(struct bmp180_inf *inf)
455 {
456         long X1, X2, X3, B3, B5, B6, p;
457         unsigned long B4, B7;
458         long pressure;
459
460         X1 = ((inf->UT - inf->rom.ac6) * inf->rom.ac5) >> 15;
461         X2 = inf->rom.mc * (1 << 11) / (X1 + inf->rom.md);
462         B5 = X1 + X2;
463         inf->temperature = (B5 + 8) >> 4;
464         B6 = B5 - 4000;
465         X1 = (inf->rom.b2 * ((B6 * B6) >> 12)) >> 11;
466         X2 = (inf->rom.ac2 * B6) >> 11;
467         X3 = X1 + X2;
468         B3 = ((((inf->rom.ac1 << 2) + X3) << inf->range_index) + 2) >> 2;
469         X1 = (inf->rom.ac3 * B6) >> 13;
470         X2 = (inf->rom.b1 * ((B6 * B6) >> 12)) >> 16;
471         X3 = ((X1 + X2) + 2) >> 2;
472         B4 = (inf->rom.ac4 * (unsigned long)(X3 + 32768)) >> 15;
473         B7 = ((unsigned long)inf->UP - B3) * (50000 >> inf->range_index);
474         if (B7 < 0x80000000)
475                 p = (B7 << 1) / B4;
476         else
477                 p = (B7 / B4) << 1;
478         X1 = (p >> 8) * (p >> 8);
479         X1 = (X1 * 3038) >> 16;
480         X2 = (-7357 * p) >> 16;
481         pressure = p + ((X1 + X2 + 3791) >> 4);
482         inf->pressure = (int)pressure;
483 }
484
485 static void bmp180_report(struct bmp180_inf *inf, u8 *data, s64 ts)
486 {
487         input_report_abs(inf->idev, ABS_PRESSURE, inf->pressure);
488         input_sync(inf->idev);
489 }
490
491 static int bmp180_read_sts(struct bmp180_inf *inf, u8 *data)
492 {
493         long val;
494         int limit_lo;
495         int limit_hi;
496         int pres;
497         bool report;
498         int err = 0;
499
500         /* BMP180_REG_CTRL_SCO is 0 when data is ready */
501         val = data[0] & (1 << BMP180_REG_CTRL_SCO);
502         if (!val) {
503                 err = -1;
504                 if (data[0] == 0x0A) { /* temperature */
505                         inf->UT = ((data[2] << 8) + data[3]);
506                         inf->data_out = BMP180_REG_CTRL_MODE_PRES |
507                                      (inf->range_index << BMP180_REG_CTRL_OSS);
508                 } else { /* pressure */
509                         val = ((data[2] << 16) + (data[3] << 8) + data[4]) >>
510                                                         (8 - inf->range_index);
511                         inf->data_out = BMP180_REG_CTRL_MODE_TEMP;
512                         if (inf->resolution && (!inf->report)) {
513                                 if (inf->UP == val)
514                                         return err;
515                         }
516
517                         inf->UP = val;
518                         bmp180_calc(inf);
519                         pres = inf->pressure;
520                         if (inf->resolution) {
521                                 limit_lo = pres;
522                                 limit_hi = pres;
523                                 limit_lo -= (inf->resolution / 2);
524                                 limit_hi += (inf->resolution / 2);
525                                 if (limit_lo < 0)
526                                         limit_lo = 0;
527                                 if ((pres < limit_lo) || (pres > limit_hi))
528                                         report = true;
529                                 else
530                                         report = false;
531                         } else {
532                                 report = true;
533                         }
534                         if (report || inf->report) {
535                                 inf->report = false;
536                                 err = 1;
537                         }
538                 }
539         }
540         return err;
541 }
542
543 static s64 bmp180_timestamp_ns(void)
544 {
545         struct timespec ts;
546         s64 ns;
547
548         ktime_get_ts(&ts);
549         ns = timespec_to_ns(&ts);
550         return ns;
551 }
552
553 static int bmp180_read(struct bmp180_inf *inf)
554 {
555         long long timestamp1;
556         long long timestamp2;
557         u8 data[5];
558         int err;
559
560         timestamp1 = bmp180_timestamp_ns();
561         err = bmp180_i2c_rd(inf, BMP180_REG_CTRL, 5, data);
562         timestamp2 = bmp180_timestamp_ns();
563         if (err)
564                 return err;
565
566         err = bmp180_read_sts(inf, data);
567         if (err > 0) {
568                 timestamp2 = (timestamp2 - timestamp1) / 2;
569                 timestamp1 += timestamp2;
570                 bmp180_report(inf, data, timestamp1);
571                 bmp180_i2c_wr(inf, BMP180_REG_CTRL, inf->data_out);
572         } else if (err < 0) {
573                 bmp180_i2c_wr(inf, BMP180_REG_CTRL, inf->data_out);
574         }
575         return err;
576 }
577
578 static void bmp180_mpu_handler(u8 *data, unsigned int len, s64 ts, void *p_val)
579 {
580         struct bmp180_inf *inf;
581         int err;
582
583         inf = (struct bmp180_inf *)p_val;
584         if (inf->enable) {
585                 err = bmp180_read_sts(inf, data);
586                 if (err > 0) {
587                         bmp180_report(inf, data, ts);
588                         nvi_mpu_data_out(inf->port_id[WR], inf->data_out);
589                 } else if (err < 0) {
590                         nvi_mpu_data_out(inf->port_id[WR], inf->data_out);
591                 }
592         }
593 }
594
595 static int bmp180_id(struct bmp180_inf *inf)
596 {
597         struct nvi_mpu_port nmp;
598         u8 config_boot;
599         u8 val = 0;
600         int err;
601
602         config_boot = inf->pdata.config & NVI_CONFIG_BOOT_MASK;
603         if (config_boot == NVI_CONFIG_BOOT_AUTO) {
604                 nmp.addr = BMP180_I2C_ADDR | 0x80;
605                 nmp.reg = BMP180_REG_ID;
606                 nmp.ctrl = 1;
607                 err = nvi_mpu_dev_valid(&nmp, &val);
608                 /* see mpu.h for possible return values */
609                 if ((err == -EAGAIN) || (err == -EBUSY))
610                         return -EAGAIN;
611
612                 if (((!err) && (val == BMP180_REG_ID_VAL)) || (err == -EIO))
613                         config_boot = NVI_CONFIG_BOOT_MPU;
614         }
615         if (config_boot == NVI_CONFIG_BOOT_MPU) {
616                 inf->use_mpu = true;
617                 nmp.addr = BMP180_I2C_ADDR | 0x80;
618                 nmp.reg = BMP180_REG_CTRL;
619                 nmp.ctrl = 5;
620                 nmp.data_out = 0;
621                 nmp.delay_ms = 0;
622                 nmp.delay_us = inf->poll_delay_us;
623                 nmp.shutdown_bypass = false;
624                 nmp.handler = &bmp180_mpu_handler;
625                 nmp.ext_driver = (void *)inf;
626                 err = nvi_mpu_port_alloc(&nmp);
627                 if (err < 0)
628                         return err;
629
630                 inf->port_id[RD] = err;
631                 nmp.addr = BMP180_I2C_ADDR;
632                 nmp.reg = BMP180_REG_CTRL;
633                 nmp.ctrl = 1;
634                 nmp.data_out = BMP180_REG_CTRL_MODE_TEMP;
635                 nmp.delay_ms = bmp180_delay_ms_tbl[inf->range_index];
636                 nmp.delay_us = 0;
637                 nmp.shutdown_bypass = false;
638                 nmp.handler = NULL;
639                 nmp.ext_driver = NULL;
640                 err = nvi_mpu_port_alloc(&nmp);
641                 if (err < 0) {
642                         bmp180_ports_free(inf);
643                         dev_err(&inf->i2c->dev, "%s ERR %d", __func__, err);
644                 } else {
645                         inf->port_id[WR] = err;
646                         err = 0;
647                 }
648                 return err;
649         }
650
651         /* NVI_CONFIG_BOOT_EXTERNAL */
652         inf->use_mpu = false;
653         err = bmp180_i2c_rd(inf, BMP180_REG_ID, 1, &val);
654         if ((!err) && (val == BMP180_REG_ID_VAL))
655                 return 0;
656
657         return -ENODEV;
658 }
659
660 static void bmp180_work(struct work_struct *ws)
661 {
662         struct bmp180_inf *inf;
663
664         inf = container_of(ws, struct bmp180_inf, dw.work);
665         bmp180_read(inf);
666         queue_delayed_work(inf->wq, &inf->dw,
667                            usecs_to_jiffies(inf->poll_delay_us));
668 }
669
670 static int bmp180_enable(struct bmp180_inf *inf, bool enable)
671 {
672         int err = 0;
673
674         bmp180_pm(inf, true);
675         if (!inf->initd)
676                 err = bmp180_init_hw(inf);
677         if (enable) {
678                 inf->report = true;
679                 err |= bmp180_delay(inf, inf->poll_delay_us);
680                 err |= bmp180_reset(inf);
681                 if (!err)
682                         inf->enable = true;
683         } else {
684                 inf->enable = false;
685                 if (inf->use_mpu)
686                         err = bmp180_ports_enable(inf, false);
687                 else
688                         cancel_delayed_work_sync(&inf->dw);
689                 if (!err)
690                         bmp180_pm(inf, false);
691         }
692         return err;
693 }
694
695 static ssize_t bmp180_enable_store(struct device *dev,
696                                    struct device_attribute *attr,
697                                    const char *buf, size_t count)
698 {
699         struct bmp180_inf *inf;
700         unsigned int enable;
701         bool en;
702         int err;
703
704         inf = dev_get_drvdata(dev);
705         err = kstrtouint(buf, 10, &enable);
706         if (err)
707                 return -EINVAL;
708
709         if (enable)
710                 en = true;
711         else
712                 en = false;
713         dev_dbg(&inf->i2c->dev, "%s: %x", __func__, en);
714         err = bmp180_enable(inf, en);
715         if (err) {
716                 dev_err(&inf->i2c->dev, "%s: %x ERR=%d", __func__, en, err);
717                 return err;
718         }
719
720         return count;
721 }
722
723 static ssize_t bmp180_enable_show(struct device *dev,
724                                   struct device_attribute *attr, char *buf)
725 {
726         struct bmp180_inf *inf;
727         unsigned int enable = 0;
728
729         inf = dev_get_drvdata(dev);
730         if (inf->enable)
731                 enable = 1;
732         return sprintf(buf, "%u\n", enable);
733 }
734
735 static ssize_t bmp180_delay_store(struct device *dev,
736                                   struct device_attribute *attr,
737                                   const char *buf, size_t count)
738 {
739         struct bmp180_inf *inf;
740         unsigned long delay_us;
741         int err;
742
743         inf = dev_get_drvdata(dev);
744         err = kstrtoul(buf, 10, &delay_us);
745         if (err)
746                 return -EINVAL;
747
748         dev_dbg(&inf->i2c->dev, "%s: %lu\n", __func__, delay_us);
749         /* since we rotate between acquiring data for pressure and temperature
750          * we need to go twice as fast.
751          */
752         delay_us >>= 1;
753         if (delay_us < (bmp180_delay_ms_tbl[inf->range_index] * 1000))
754                 delay_us = (bmp180_delay_ms_tbl[inf->range_index] * 1000);
755         if (inf->enable && (delay_us != inf->poll_delay_us))
756                 err = bmp180_delay(inf, delay_us);
757         if (!err) {
758                 inf->poll_delay_us = delay_us;
759         } else {
760                 dev_err(&inf->i2c->dev, "%s: %lu ERR=%d\n",
761                         __func__, delay_us, err);
762                 return err;
763         }
764
765         return count;
766 }
767
768 static ssize_t bmp180_delay_show(struct device *dev,
769                                  struct device_attribute *attr, char *buf)
770 {
771         struct bmp180_inf *inf;
772         unsigned long delay_us;
773
774         inf = dev_get_drvdata(dev);
775         if (inf->enable)
776                 delay_us = inf->poll_delay_us;
777         else
778                 delay_us = bmp180_delay_ms_tbl[inf->range_index] * 1000;
779         return sprintf(buf, "%lu\n", delay_us);
780 }
781
782 static ssize_t bmp180_resolution_store(struct device *dev,
783                                        struct device_attribute *attr,
784                                        const char *buf, size_t count)
785 {
786         struct bmp180_inf *inf;
787         unsigned int resolution;
788
789         inf = dev_get_drvdata(dev);
790         if (kstrtouint(buf, 10, &resolution))
791                 return -EINVAL;
792
793         inf->resolution = resolution;
794         dev_dbg(&inf->i2c->dev, "%s %u", __func__, resolution);
795         return count;
796 }
797
798 static ssize_t bmp180_resolution_show(struct device *dev,
799                                       struct device_attribute *attr, char *buf)
800 {
801         struct bmp180_inf *inf;
802         unsigned int resolution;
803
804         inf = dev_get_drvdata(dev);
805         if (inf->enable)
806                 resolution = inf->resolution;
807         else
808                 resolution = BMP180_INPUT_RESOLUTION;
809         return sprintf(buf, "%u\n", resolution);
810 }
811
812 static ssize_t bmp180_max_range_store(struct device *dev,
813                                       struct device_attribute *attr,
814                                       const char *buf, size_t count)
815 {
816         struct bmp180_inf *inf;
817         u8 range_index;
818         int err;
819
820         inf = dev_get_drvdata(dev);
821         err = kstrtou8(buf, 10, &range_index);
822         if (err)
823                 return -EINVAL;
824
825         if (range_index > 3)
826                 return -EINVAL;
827
828         dev_dbg(&inf->i2c->dev, "%s %u", __func__, range_index);
829         inf->range_index = range_index;
830         nvi_mpu_delay_ms(inf->port_id[WR], bmp180_delay_ms_tbl[range_index]);
831         return count;
832 }
833
834 static ssize_t bmp180_max_range_show(struct device *dev,
835                                      struct device_attribute *attr, char *buf)
836 {
837         struct bmp180_inf *inf;
838         unsigned int range;
839
840         inf = dev_get_drvdata(dev);
841         if (inf->enable)
842                 range = inf->range_index;
843         else
844                 range = (BMP180_PRESSURE_MAX * BMP180_INPUT_DIVISOR);
845         return sprintf(buf, "%u\n", range);
846 }
847
848 static ssize_t bmp180_divisor_show(struct device *dev,
849                                    struct device_attribute *attr, char *buf)
850 {
851         struct bmp180_inf *inf;
852
853         inf = dev_get_drvdata(dev);
854         return sprintf(buf, "%d\n", BMP180_INPUT_DIVISOR);
855 }
856
857 static ssize_t bmp180_microamp_show(struct device *dev,
858                                     struct device_attribute *attr, char *buf)
859 {
860         struct bmp180_inf *inf;
861
862         inf = dev_get_drvdata(dev);
863         return sprintf(buf, "%d\n", BMP180_INPUT_POWER_UA);
864 }
865
866 static ssize_t bmp180_pressure_show(struct device *dev,
867                                     struct device_attribute *attr, char *buf)
868 {
869         struct bmp180_inf *inf;
870
871         inf = dev_get_drvdata(dev);
872         if (inf->enable)
873                 return sprintf(buf, "%d\n", inf->pressure);
874
875         return -EPERM;
876 }
877
878 static ssize_t bmp180_temperature_show(struct device *dev,
879                                        struct device_attribute *attr,
880                                        char *buf)
881 {
882         struct bmp180_inf *inf;
883
884         inf = dev_get_drvdata(dev);
885         if (inf->enable)
886                 return sprintf(buf, "%ld\n", inf->temperature);
887
888         return -EPERM;
889 }
890
891 static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR | S_IWOTH,
892                    bmp180_enable_show, bmp180_enable_store);
893 static DEVICE_ATTR(delay, S_IRUGO | S_IWUSR | S_IWOTH,
894                    bmp180_delay_show, bmp180_delay_store);
895 static DEVICE_ATTR(resolution, S_IRUGO | S_IWUSR | S_IWOTH,
896                    bmp180_resolution_show, bmp180_resolution_store);
897 static DEVICE_ATTR(max_range, S_IRUGO | S_IWUSR | S_IWOTH,
898                    bmp180_max_range_show, bmp180_max_range_store);
899 static DEVICE_ATTR(divisor, S_IRUGO,
900                    bmp180_divisor_show, NULL);
901 static DEVICE_ATTR(microamp, S_IRUGO,
902                    bmp180_microamp_show, NULL);
903 static DEVICE_ATTR(pressure, S_IRUGO,
904                    bmp180_pressure_show, NULL);
905 static DEVICE_ATTR(temperature, S_IRUGO,
906                    bmp180_temperature_show, NULL);
907
908 static struct attribute *bmp180_attrs[] = {
909         &dev_attr_enable.attr,
910         &dev_attr_delay.attr,
911         &dev_attr_resolution.attr,
912         &dev_attr_max_range.attr,
913         &dev_attr_divisor.attr,
914         &dev_attr_microamp.attr,
915         &dev_attr_pressure.attr,
916         &dev_attr_temperature.attr,
917         NULL
918 };
919
920 static struct attribute_group bmp180_attr_group = {
921         .name = BMP180_NAME,
922         .attrs = bmp180_attrs
923 };
924
925 static int bmp180_sysfs_create(struct bmp180_inf *inf)
926 {
927         int err;
928
929         err = sysfs_create_group(&inf->idev->dev.kobj, &bmp180_attr_group);
930         return err;
931 }
932
933 static void bmp180_input_close(struct input_dev *idev)
934 {
935         struct bmp180_inf *inf;
936
937         inf = input_get_drvdata(idev);
938         if (inf != NULL)
939                 bmp180_enable(inf, false);
940 }
941
942 static int bmp180_input_create(struct bmp180_inf *inf)
943 {
944         int err;
945
946         inf->idev = input_allocate_device();
947         if (!inf->idev) {
948                 err = -ENOMEM;
949                 dev_err(&inf->i2c->dev, "%s ERR %d\n", __func__, err);
950                 return err;
951         }
952
953         inf->idev->name = BMP180_NAME;
954         inf->idev->dev.parent = &inf->i2c->dev;
955         inf->idev->close = bmp180_input_close;
956         input_set_drvdata(inf->idev, inf);
957         input_set_capability(inf->idev, EV_ABS, ABS_PRESSURE);
958         input_set_abs_params(inf->idev, ABS_PRESSURE,
959                              BMP180_PRESSURE_MIN, BMP180_PRESSURE_MAX,
960                              BMP180_PRESSURE_FUZZ, BMP180_PRESSURE_FLAT);
961         err = input_register_device(inf->idev);
962         if (err) {
963                 input_free_device(inf->idev);
964                 inf->idev = NULL;
965         }
966         return err;
967 }
968
969 static int bmp180_remove(struct i2c_client *client)
970 {
971         struct bmp180_inf *inf;
972
973         inf = i2c_get_clientdata(client);
974         if (inf != NULL) {
975                 if (inf->idev) {
976                         input_unregister_device(inf->idev);
977                         input_free_device(inf->idev);
978                 }
979                 if (inf->wq)
980                         destroy_workqueue(inf->wq);
981                 bmp180_pm_exit(inf);
982                 kfree(inf);
983         }
984         dev_info(&client->dev, "%s\n", __func__);
985         return 0;
986 }
987
988 static void bmp180_shutdown(struct i2c_client *client)
989 {
990         bmp180_remove(client);
991 }
992
993 static int bmp180_probe(struct i2c_client *client,
994                         const struct i2c_device_id *id)
995 {
996         struct bmp180_inf *inf;
997         struct mpu_platform_data *pd;
998         int err;
999
1000         dev_info(&client->dev, "%s\n", __func__);
1001         inf = kzalloc(sizeof(*inf), GFP_KERNEL);
1002         if (IS_ERR_OR_NULL(inf)) {
1003                 dev_err(&client->dev, "%s kzalloc ERR\n", __func__);
1004                 return -ENOMEM;
1005         }
1006
1007         inf->i2c = client;
1008         i2c_set_clientdata(client, inf);
1009         pd = (struct mpu_platform_data *)dev_get_platdata(&client->dev);
1010         if (pd != NULL)
1011                 inf->pdata = *pd;
1012         bmp180_pm_init(inf);
1013         err = bmp180_id(inf);
1014         bmp180_pm(inf, false);
1015         if (err == -EAGAIN)
1016                 goto bmp180_probe_again;
1017         else if (err)
1018                 goto bmp180_probe_err;
1019
1020         err = bmp180_input_create(inf);
1021         if (err)
1022                 goto bmp180_probe_err;
1023
1024         inf->wq = create_singlethread_workqueue(BMP180_NAME);
1025         if (!inf->wq) {
1026                 dev_err(&client->dev, "%s workqueue ERR\n", __func__);
1027                 err = -ENOMEM;
1028                 goto bmp180_probe_err;
1029         }
1030
1031         INIT_DELAYED_WORK(&inf->dw, bmp180_work);
1032         err = bmp180_sysfs_create(inf);
1033         if (err)
1034                 goto bmp180_probe_err;
1035
1036         return 0;
1037
1038 bmp180_probe_err:
1039         dev_err(&client->dev, "%s ERR %d\n", __func__, err);
1040 bmp180_probe_again:
1041         bmp180_remove(client);
1042         return err;
1043 }
1044
1045 static const struct i2c_device_id bmp180_i2c_device_id[] = {
1046         {BMP180_NAME, 0},
1047         {}
1048 };
1049
1050 MODULE_DEVICE_TABLE(i2c, bmp180_i2c_device_id);
1051
1052 static struct i2c_driver bmp180_driver = {
1053         .class          = I2C_CLASS_HWMON,
1054         .probe          = bmp180_probe,
1055         .remove         = bmp180_remove,
1056         .driver = {
1057                 .name   = BMP180_NAME,
1058                 .owner  = THIS_MODULE,
1059         },
1060         .id_table       = bmp180_i2c_device_id,
1061         .shutdown       = bmp180_shutdown,
1062 };
1063
1064 static int __init bmp180_init(void)
1065 {
1066         return i2c_add_driver(&bmp180_driver);
1067 }
1068
1069 static void __exit bmp180_exit(void)
1070 {
1071         i2c_del_driver(&bmp180_driver);
1072 }
1073
1074 module_init(bmp180_init);
1075 module_exit(bmp180_exit);
1076
1077 MODULE_LICENSE("GPL");
1078 MODULE_DESCRIPTION("BMP180 driver");
1079 MODULE_AUTHOR("NVIDIA Corp");
1080