iio: common: NVS v209 common timestamps
[linux-3.10.git] / drivers / iio / pressure / nvi_bmpX80.c
1 /* Copyright (c) 2014-2015, 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 /* Device mapping is done via three parameters:
14  * 1. If BMP_NVI_MPU_SUPPORT (defined below) is set, the code is included to
15  *    support the device behind an Invensense MPU running an NVI (NVidia/
16  *    Invensense) driver.
17  *    If BMP_NVI_MPU_SUPPORT is 0 then this driver is only for the device in a
18  *    stand-alone configuration without any dependancies on an Invensense MPU.
19  * 2. Device tree platform configuration nvi_config:
20  *    - auto = automatically detect if connected to host or MPU
21  *    - mpu = connected to MPU
22  *    - host = connected to host
23  *    This is only available if BMP_NVI_MPU_SUPPORT is set.
24  * 3. device in board file:
25  *    - bmpX80 = automatically detect the device
26  *    - force the device for:
27  *      - bmp180
28  *      - bmp280
29  * If you have no clue what the device is and don't know how it is
30  * connected then use auto and bmpX80.  The auto-detect mechanisms are for
31  * platforms that have multiple possible configurations but takes longer to
32  * initialize.  No device identification and connect testing is done for
33  * specific configurations.
34  */
35 /* A defined interrupt can be used as a SW flag to configure the device if
36  * behind the MPU.  When an interrupt is defined in struct i2c_client.irq,
37  * the driver is configured to only use the device's continuous mode if the
38  * device supports it.  The interrupt itself is never touched so any value can
39  * be used.  This frees the MPU auxiliary port used for writes.  This
40  * configuration would be used if another MPU auxiliary port was needed for
41  * another device connected to the MPU.  The delay timing used in continuous
42  * mode is equal to or the next fastest supported speed.
43  */
44 /* The NVS = NVidia Sensor framework */
45 /* NVI = NVidia/Invensense */
46 /* See Nvs.cpp in the HAL for the NVS implementation of batch/flush. */
47 /* See NvsIio.cpp in the HAL for the IIO enable/disable extension mechanism. */
48
49
50 #include <linux/i2c.h>
51 #include <linux/module.h>
52 #include <linux/slab.h>
53 #include <linux/err.h>
54 #include <linux/delay.h>
55 #include <linux/regulator/consumer.h>
56 #include <linux/workqueue.h>
57 #include <linux/of.h>
58 #include <linux/nvs.h>
59 #if BMP_NVI_MPU_SUPPORT
60 #include <linux/mpu_iio.h>
61 #endif /* BMP_NVI_MPU_SUPPORT */
62
63 #define BMP_VERSION_DRIVER              (103)
64 #define BMP_VENDOR                      "Bosch"
65 #define BMP_NAME                        "bmpX80"
66 #define BMP180_NAME                     "bmp180"
67 #define BMP280_NAME                     "bmp280"
68 #define BMP_KBUF_SIZE                   (32)
69 #define BMP_PRES_MAX_RANGE_IVAL         (1100)
70 #define BMP_PRES_MAX_RANGE_MICRO        (0)
71 #define BMP_TEMP_MAX_RANGE_IVAL         (125)
72 #define BMP_TEMP_MAX_RANGE_MICRO        (0)
73 #define BMP180_RANGE_DFLT               (0)
74 /* until BMP280 OSS pressure calculation is supported, this defaults to 5 */
75 #define BMP280_RANGE_DFLT               (5)
76 #define BMP_HW_DELAY_POR_MS             (10)
77 #define BMP_POLL_DELAY_MS_DFLT          (200)
78 #define BMP_MPU_RETRY_COUNT             (50)
79 #define BMP_MPU_RETRY_DELAY_MS          (20)
80 /* HW registers */
81 #define BMP_REG_ID                      (0xD0)
82 #define BMP_REG_ID_BMP180               (0x55)
83 #define BMP_REG_ID_BMP280               (0x56)
84 #define BMP_REG_RESET                   (0xE0)
85 #define BMP_REG_RESET_VAL               (0xB6)
86 #define BMP_REG_CTRL                    (0xF4)
87 #define BMP_REG_CTRL_MODE_SLEEP         (0)
88 #define BMP180_REG_CTRL_MODE_MASK       (0x1F)
89 #define BMP180_REG_CTRL_MODE_PRES       (0x34)
90 #define BMP180_REG_CTRL_MODE_TEMP       (0x2E)
91 #define BMP180_REG_CTRL_SCO             (5)
92 #define BMP180_REG_CTRL_OSS             (6)
93 #define BMP280_REG_CTRL_MODE_MASK       (0x03)
94 #define BMP280_REG_CTRL_MODE_FORCED1    (1)
95 #define BMP280_REG_CTRL_MODE_FORCED2    (2)
96 #define BMP280_REG_CTRL_MODE_NORMAL     (3)
97 #define BMP280_REG_CTRL_OSRS_P          (2)
98 #define BMP280_REG_CTRL_OSRS_P_MASK     (0x1C)
99 #define BMP280_REG_CTRL_OSRS_T          (5)
100 #define BMP280_REG_CTRL_OSRS_T_MASK     (0xE0)
101 #define BMP180_REG_OUT_MSB              (0xF6)
102 #define BMP180_REG_OUT_LSB              (0xF7)
103 #define BMP180_REG_OUT_XLSB             (0xF8)
104 #define BMP280_REG_STATUS               (0xF3)
105 #define BMP280_REG_STATUS_MEASURING     (3)
106 #define BMP280_REG_STATUS_IM_UPDATE     (0)
107 #define BMP280_REG_CONFIG               (0xF5)
108 #define BMP280_REG_CONFIG_T_SB          (5)
109 #define BMP280_REG_PRESS_MSB            (0xF7)
110 #define BMP280_REG_PRESS_LSB            (0xF8)
111 #define BMP280_REG_PRESS_XLSB           (0xF9)
112 #define BMP280_REG_TEMP_MSB             (0xFA)
113 #define BMP280_REG_TEMP_LSB             (0xFB)
114 #define BMP280_REG_TEMP_XLSB            (0xFC)
115 /* ROM registers */
116 #define BMP180_REG_AC1                  (0xAA)
117 #define BMP180_REG_AC2                  (0xAC)
118 #define BMP180_REG_AC3                  (0xAE)
119 #define BMP180_REG_AC4                  (0xB0)
120 #define BMP180_REG_AC5                  (0xB2)
121 #define BMP180_REG_AC6                  (0xB4)
122 #define BMP180_REG_B1                   (0xB6)
123 #define BMP180_REG_B2                   (0xB8)
124 #define BMP180_REG_MB                   (0xBA)
125 #define BMP180_REG_MC                   (0xBC)
126 #define BMP180_REG_MD                   (0xBE)
127 #define BMP280_REG_CWORD00              (0x88)
128 #define BMP280_REG_CWORD01              (0x8A)
129 #define BMP280_REG_CWORD02              (0x8C)
130 #define BMP280_REG_CWORD03              (0x8E)
131 #define BMP280_REG_CWORD04              (0x90)
132 #define BMP280_REG_CWORD05              (0x92)
133 #define BMP280_REG_CWORD06              (0x94)
134 #define BMP280_REG_CWORD07              (0x96)
135 #define BMP280_REG_CWORD08              (0x98)
136 #define BMP280_REG_CWORD09              (0x9A)
137 #define BMP280_REG_CWORD10              (0x9C)
138 #define BMP280_REG_CWORD11              (0x9E)
139 #define BMP280_REG_CWORD12              (0xA0)
140
141 #define BATCH_DRY_RUN                   (0x1)
142 #define BATCH_WAKE_UPON_FIFO_FULL       (0x2)
143 #define WR                              (0)
144 #define RD                              (1)
145 /* _buf_push expects this scan order */
146 #define BMP_DEV_PRES                    (0)
147 #define BMP_DEV_TEMP                    (1)
148 #define BMP_DEV_N                       (2)
149
150 /* regulator names in order of powering on */
151 static char *bmp_vregs[] = {
152         "vdd",
153         "vddio",
154 };
155
156 static char *bmp_configs[] = {
157         "auto",
158         "mpu",
159         "host",
160 };
161
162 static u8 bmp_ids[] = {
163         BMP_REG_ID_BMP180,
164         BMP_REG_ID_BMP280,
165         0x57,
166         0x58,
167 };
168
169 static unsigned short bmp_i2c_addrs[] = {
170         0x76,
171         0x77,
172 };
173
174 struct bmp_scale {
175         unsigned long delay_ms;
176         struct nvs_float resolution;
177         struct nvs_float milliamp;
178 };
179
180 struct bmp_hal_dev {
181         int version;
182         unsigned int scale_i_max;
183         unsigned int scale_dflt;
184         struct bmp_scale *scale;
185         struct nvs_float scale_float;
186 };
187
188 union bmp_rom {
189         struct bmp180_rom {
190                 s16 ac1;
191                 s16 ac2;
192                 s16 ac3;
193                 u16 ac4;
194                 u16 ac5;
195                 u16 ac6;
196                 s16 b1;
197                 s16 b2;
198                 s16 mb;
199                 s16 mc;
200                 s16 md;
201         } bmp180;
202         struct bmp280_rom {
203                 u16 dig_T1;
204                 s16 dig_T2;
205                 s16 dig_T3;
206                 u16 dig_P1;
207                 s16 dig_P2;
208                 s16 dig_P3;
209                 s16 dig_P4;
210                 s16 dig_P5;
211                 s16 dig_P6;
212                 s16 dig_P7;
213                 s16 dig_P8;
214                 s16 dig_P9;
215                 s16 reserved;
216         } bmp280;
217 } rom;
218
219 struct bmp_cmode {
220         unsigned int t_us;
221         u8 t_sb;
222 };
223
224 struct bmp_state {
225         struct i2c_client *i2c;
226         struct nvs_fn_if *nvs;
227         void *nvs_st[BMP_DEV_N];
228         struct sensor_cfg cfg[BMP_DEV_N];
229         struct regulator_bulk_data vreg[ARRAY_SIZE(bmp_vregs)];
230         struct delayed_work dw;
231         struct bmp_hal *hal;            /* Hardware Abstaction Layer */
232         union bmp_rom rom;              /* calibration data */
233         unsigned int sts;               /* status flags */
234         unsigned int errs;              /* error count */
235         unsigned int enabled;           /* enable status */
236         unsigned int poll_delay_us;     /* global sampling delay */
237         unsigned int delay_us[BMP_DEV_N]; /* sampling delay */
238         unsigned int scale_i;           /* oversampling index */
239         unsigned int scale_user;        /* user oversampling index */
240         u16 i2c_addr;                   /* I2C address */
241         u8 dev_id;                      /* device ID */
242         bool initd;                     /* set if initialized */
243         bool mpu_en;                    /* if device behind MPU */
244         bool port_en[2];                /* enable status of MPU write port */
245         int port_id[2];                 /* MPU port ID */
246         u8 data_out;                    /* write value to mode register */
247         s32 ut;                         /* uncompensated temperature */
248         s32 up;                         /* uncompensated pressure */
249         s32 t_fine;                     /* temperature used in pressure calc */
250         s32 temp;                       /* true temperature */
251         int pressure;                   /* true pressure hPa/100 Pa/1 mBar */
252         s64 ts;                         /* sample data timestamp */
253         u8 nvi_config;                  /* NVI configuration */
254 };
255
256 struct bmp_hal {
257         struct bmp_hal_dev *p;
258         struct bmp_hal_dev *t;
259         const char *part;
260         u8 rom_addr_start;
261         u8 rom_size;
262         bool rom_big_endian;
263         u8 mode_mask;
264         struct bmp_cmode *cmode_tbl;
265         int (*bmp_read)(struct bmp_state *st);
266         unsigned int mpu_id;
267 };
268
269
270 static void bmp_err(struct bmp_state *st)
271 {
272         st->errs++;
273         if (!st->errs)
274                 st->errs--;
275 }
276
277 static int bmp_i2c_rd(struct bmp_state *st, u8 reg, u16 len, u8 *val)
278 {
279         struct i2c_msg msg[2];
280
281         msg[0].addr = st->i2c_addr;
282         msg[0].flags = 0;
283         msg[0].len = 1;
284         msg[0].buf = &reg;
285         msg[1].addr = st->i2c_addr;
286         msg[1].flags = I2C_M_RD;
287         msg[1].len = len;
288         msg[1].buf = val;
289         if (i2c_transfer(st->i2c->adapter, msg, 2) != 2) {
290                 bmp_err(st);
291                 return -EIO;
292         }
293
294         return 0;
295 }
296
297 static int bmp_i2c_wr(struct bmp_state *st, u8 reg, u8 val)
298 {
299         struct i2c_msg msg;
300         u8 buf[2];
301
302         if (st->i2c_addr) {
303                 buf[0] = reg;
304                 buf[1] = val;
305                 msg.addr = st->i2c_addr;
306                 msg.flags = 0;
307                 msg.len = 2;
308                 msg.buf = buf;
309                 if (i2c_transfer(st->i2c->adapter, &msg, 1) != 1) {
310                         bmp_err(st);
311                         return -EIO;
312                 }
313         }
314
315         return 0;
316 }
317
318 static int bmp_nvi_mpu_bypass_request(struct bmp_state *st)
319 {
320         int ret = 0;
321 #if BMP_NVI_MPU_SUPPORT
322         int i;
323
324         if (st->mpu_en) {
325                 for (i = 0; i < BMP_MPU_RETRY_COUNT; i++) {
326                         ret = nvi_mpu_bypass_request(true);
327                         if ((!ret) || (ret == -EPERM))
328                                 break;
329
330                         msleep(BMP_MPU_RETRY_DELAY_MS);
331                 }
332                 if (ret == -EPERM)
333                         ret = 0;
334         }
335 #endif /* BMP_NVI_MPU_SUPPORT */
336         return ret;
337 }
338
339 static int bmp_nvi_mpu_bypass_release(struct bmp_state *st)
340 {
341         int ret = 0;
342
343 #if BMP_NVI_MPU_SUPPORT
344         if (st->mpu_en)
345                 ret = nvi_mpu_bypass_release();
346 #endif /* BMP_NVI_MPU_SUPPORT */
347         return ret;
348 }
349
350 static int bmp_mode_wr(struct bmp_state *st, u8 mode)
351 {
352         int ret;
353
354 #if BMP_NVI_MPU_SUPPORT
355         if (st->mpu_en && !st->i2c->irq) {
356                 ret = nvi_mpu_data_out(st->port_id[WR], mode);
357         } else {
358                 ret = bmp_nvi_mpu_bypass_request(st);
359                 if (!ret) {
360                         if (st->i2c->irq) {
361                                 ret = bmp_i2c_wr(st, BMP_REG_CTRL,
362                                                  BMP_REG_CTRL_MODE_SLEEP);
363                                 if (mode & st->hal->mode_mask) {
364                                         udelay(BMP_HW_DELAY_POR_MS);
365                                         ret |= bmp_i2c_wr(st, BMP_REG_CTRL,
366                                                           mode);
367                                 }
368                         } else {
369                                 ret = bmp_i2c_wr(st, BMP_REG_CTRL, mode);
370                         }
371                         bmp_nvi_mpu_bypass_release(st);
372                 }
373         }
374 #else /* BMP_NVI_MPU_SUPPORT */
375         ret = bmp_i2c_wr(st, BMP_REG_CTRL, mode);
376 #endif /* BMP_NVI_MPU_SUPPORT */
377         if (!ret)
378                 st->data_out = mode;
379         return ret;
380 }
381
382 static int bmp_pm(struct bmp_state *st, bool enable)
383 {
384         int ret = 0;
385
386         if (enable) {
387                 ret = nvs_vregs_enable(&st->i2c->dev, st->vreg,
388                                        ARRAY_SIZE(bmp_vregs));
389                 if (ret)
390                         mdelay(BMP_HW_DELAY_POR_MS);
391         } else {
392                 if (st->i2c->irq) {
393                         ret = nvs_vregs_sts(st->vreg, ARRAY_SIZE(bmp_vregs));
394                         if ((ret < 0) || (ret == ARRAY_SIZE(bmp_vregs))) {
395                                 ret = bmp_mode_wr(st, BMP_REG_CTRL_MODE_SLEEP);
396                         } else if (ret > 0) {
397                                 nvs_vregs_enable(&st->i2c->dev, st->vreg,
398                                                  ARRAY_SIZE(bmp_vregs));
399                                 mdelay(BMP_HW_DELAY_POR_MS);
400                                 ret = bmp_mode_wr(st, BMP_REG_CTRL_MODE_SLEEP);
401                         }
402                 }
403                 ret |= nvs_vregs_disable(&st->i2c->dev, st->vreg,
404                                          ARRAY_SIZE(bmp_vregs));
405         }
406         if (ret > 0)
407                 ret = 0;
408         if (ret) {
409                 dev_err(&st->i2c->dev, "%s pwr=%x ERR=%d\n",
410                         __func__, enable, ret);
411         } else {
412                 if (st->sts & NVS_STS_SPEW_MSG)
413                         dev_info(&st->i2c->dev, "%s pwr=%x\n",
414                                  __func__, enable);
415         }
416         return ret;
417 }
418
419 static int bmp_port_free(struct bmp_state *st, int port)
420 {
421         int ret = 0;
422
423 #if BMP_NVI_MPU_SUPPORT
424         if (st->port_id[port] >= 0) {
425                 ret = nvi_mpu_port_free(st->port_id[port]);
426                 if (!ret)
427                         st->port_id[port] = -1;
428         }
429 #endif /* BMP_NVI_MPU_SUPPORT */
430         return ret;
431 }
432
433 static int bmp_ports_free(struct bmp_state *st)
434 {
435         int ret;
436
437         ret = bmp_port_free(st, WR);
438         ret |= bmp_port_free(st, RD);
439         return ret;
440 }
441
442 static void bmp_pm_exit(struct bmp_state *st)
443 {
444         bmp_ports_free(st);
445         bmp_pm(st, false);
446         nvs_vregs_exit(&st->i2c->dev, st->vreg, ARRAY_SIZE(bmp_vregs));
447 }
448
449 static int bmp_pm_init(struct bmp_state *st)
450 {
451         int ret;
452
453         st->enabled = 0;
454         st->delay_us[BMP_DEV_PRES] = (BMP_POLL_DELAY_MS_DFLT * 1000);
455         st->delay_us[BMP_DEV_TEMP] = (BMP_POLL_DELAY_MS_DFLT * 1000);
456         st->poll_delay_us = (BMP_POLL_DELAY_MS_DFLT * 1000);
457         st->initd = false;
458         st->mpu_en = false;
459         st->port_en[WR] = false;
460         st->port_en[RD] = false;
461         st->port_id[WR] = -1;
462         st->port_id[RD] = -1;
463         nvs_vregs_init(&st->i2c->dev,
464                        st->vreg, ARRAY_SIZE(bmp_vregs), bmp_vregs);
465         ret = bmp_pm(st, true);
466         return ret;
467 }
468
469 static int bmp_port_enable(struct bmp_state *st, int port, bool enable)
470 {
471         int ret = 0;
472
473 #if BMP_NVI_MPU_SUPPORT
474         if ((enable != st->port_en[port]) && (st->port_id[port] >= 0)) {
475                 ret = nvi_mpu_enable(st->port_id[port], enable);
476                 if (!ret)
477                         st->port_en[port] = enable;
478         }
479 #endif /* BMP_NVI_MPU_SUPPORT */
480         return ret;
481 }
482
483 static int bmp_ports_enable(struct bmp_state *st, bool enable)
484 {
485         int ret;
486
487         ret = bmp_port_enable(st, WR, enable);
488         ret |= bmp_port_enable(st, RD, enable);
489         return ret;
490 }
491
492 static int bmp_wr(struct bmp_state *st, u8 reg, u8 val)
493 {
494         int ret = 0;
495
496         ret = bmp_nvi_mpu_bypass_request(st);
497         if (!ret) {
498                 ret = bmp_i2c_wr(st, reg, val);
499                 bmp_nvi_mpu_bypass_release(st);
500         }
501         return ret;
502 }
503
504 static int bmp_mode(struct bmp_state *st)
505 {
506         u8 mode;
507         u8 t_sb;
508         unsigned int t_us;
509         unsigned int i;
510         int ret;
511
512         if (st->dev_id == BMP_REG_ID_BMP180) {
513                 mode = st->scale_i << BMP180_REG_CTRL_OSS;
514                 mode |= BMP180_REG_CTRL_MODE_TEMP;
515         } else {
516                 mode = st->scale_i + 1;
517                 mode = ((mode << BMP280_REG_CTRL_OSRS_T) |
518                         (mode << BMP280_REG_CTRL_OSRS_P));
519                 mode |= BMP280_REG_CTRL_MODE_FORCED1;
520         }
521         if (st->i2c->irq) {
522                 i = 0;
523                 t_sb = 0;
524                 while (st->hal->cmode_tbl[i].t_us) {
525                         t_sb = st->hal->cmode_tbl[i].t_sb;
526                         t_us = st->hal->cmode_tbl[i].t_us;
527                         if (st->poll_delay_us >=
528                                             st->hal->cmode_tbl[i].t_us)
529                                 break;
530
531                         i++;
532                         if (!st->mpu_en) {
533                                 t_us -= st->hal->cmode_tbl[i].t_us;
534                                 t_us >>= 1;
535                                 t_us += st->hal->cmode_tbl[i].t_us;
536                                 if (st->poll_delay_us > t_us)
537                                         break;
538                         }
539                 }
540                 t_sb <<= BMP280_REG_CONFIG_T_SB;
541                 bmp_wr(st, BMP280_REG_CONFIG, t_sb);
542                 mode |= BMP280_REG_CTRL_MODE_NORMAL;
543         }
544         ret = bmp_mode_wr(st, mode);
545         return ret;
546 }
547
548 static void bmp_calc_180(struct bmp_state *st)
549 {
550         long X1, X2, X3, B3, B5, B6, p;
551         unsigned long B4, B7;
552         long pressure;
553
554         X1 = ((st->ut - st->rom.bmp180.ac6) * st->rom.bmp180.ac5) >> 15;
555         X2 = st->rom.bmp180.mc * (1 << 11) / (X1 + st->rom.bmp180.md);
556         B5 = X1 + X2;
557         st->temp = (B5 + 8) >> 4;
558         B6 = B5 - 4000;
559         X1 = (st->rom.bmp180.b2 * ((B6 * B6) >> 12)) >> 11;
560         X2 = (st->rom.bmp180.ac2 * B6) >> 11;
561         X3 = X1 + X2;
562         B3 = ((((st->rom.bmp180.ac1 << 2) + X3) << st->scale_i) + 2) >> 2;
563         X1 = (st->rom.bmp180.ac3 * B6) >> 13;
564         X2 = (st->rom.bmp180.b1 * ((B6 * B6) >> 12)) >> 16;
565         X3 = ((X1 + X2) + 2) >> 2;
566         B4 = (st->rom.bmp180.ac4 * (unsigned long)(X3 + 32768)) >> 15;
567         B7 = ((unsigned long)st->up - B3) * (50000 >> st->scale_i);
568         if (B7 < 0x80000000)
569                 p = (B7 << 1) / B4;
570         else
571                 p = (B7 / B4) << 1;
572         X1 = (p >> 8) * (p >> 8);
573         X1 = (X1 * 3038) >> 16;
574         X2 = (-7357 * p) >> 16;
575         pressure = p + ((X1 + X2 + 3791) >> 4);
576         st->pressure = (int)pressure;
577 }
578
579 static int bmp_read_sts_180(struct bmp_state *st, u8 *data, s64 ts)
580 {
581         s32 val;
582         int ret = 0;
583
584         /* BMP180_REG_CTRL_SCO is 0 when data is ready */
585         if (!(data[0] & (1 << BMP180_REG_CTRL_SCO))) {
586                 ret = -1;
587                 if (data[0] == 0x0A) { /* temperature */
588                         st->ut = ((data[2] << 8) + data[3]);
589                         st->data_out = BMP180_REG_CTRL_MODE_PRES |
590                                         (st->scale_i << BMP180_REG_CTRL_OSS);
591                 } else { /* pressure */
592                         val = ((data[2] << 16) + (data[3] << 8) +
593                                                 data[4]) >> (8 - st->scale_i);
594                         st->data_out = BMP180_REG_CTRL_MODE_TEMP;
595                         st->up = val;
596                         bmp_calc_180(st);
597                         st->ts = ts;
598                         ret = 1;
599                 }
600         }
601         return ret;
602 }
603
604 static int bmp_read_180(struct bmp_state *st)
605 {
606         s64 ts;
607         u8 data[5];
608         int ret;
609
610         ret = bmp_i2c_rd(st, BMP_REG_CTRL, 5, data);
611         if (ret)
612                 return ret;
613
614         ts = nvs_timestamp();
615         ret = bmp_read_sts_180(st, data, ts);
616         if (ret > 0) {
617                 if (st->nvs_st[BMP_DEV_PRES])
618                         st->nvs->handler(st->nvs_st[BMP_DEV_PRES],
619                                          &st->pressure, ts);
620                 if (st->nvs_st[BMP_DEV_TEMP])
621                         st->nvs->handler(st->nvs_st[BMP_DEV_TEMP],
622                                          &st->temp, ts);
623                 bmp_i2c_wr(st, BMP_REG_CTRL, st->data_out);
624         } else if (ret < 0) {
625                 bmp_i2c_wr(st, BMP_REG_CTRL, st->data_out);
626         }
627         return 0;
628 }
629
630 static void bmp_calc_temp_280(struct bmp_state *st)
631 {
632         s32 adc_t;
633         s32 var1;
634         s32 var2;
635
636         adc_t = st->ut;
637         adc_t >>= (4 - st->scale_i);
638         var1 = adc_t >> 3;
639         var1 -= ((s32)st->rom.bmp280.dig_T1 << 1);
640         var1 *= (s32)st->rom.bmp280.dig_T2;
641         var1 >>= 11;
642         var2 = adc_t >> 4;
643         var2 -= (s32)st->rom.bmp280.dig_T1;
644         var2 *= var2;
645         var2 >>= 12;
646         var2 *= (s32)st->rom.bmp280.dig_T3;
647         var2 >>= 14;
648         st->t_fine = var1 + var2;
649         st->temp = (st->t_fine * 5 + 128) >> 8;
650 }
651
652 static int bmp_calc_pres_280(struct bmp_state *st)
653 {
654         s32 adc_p;
655         s32 var1;
656         s32 var2;
657         s32 var3;
658         u32 p;
659
660         adc_p = st->up;
661         var1 = st->t_fine >> 1;
662         var1 -= 64000;
663         var2 = var1 >> 2;
664         var2 *= var2;
665         var3 = var2;
666         var2 >>= 11;
667         var2 *= st->rom.bmp280.dig_P6;
668         var2 += ((var1 * st->rom.bmp280.dig_P5) << 1);
669         var2 >>= 2;
670         var2 += (st->rom.bmp280.dig_P4 << 16);
671         var3 >>= 13;
672         var3 *= st->rom.bmp280.dig_P3;
673         var3 >>= 3;
674         var1 *= st->rom.bmp280.dig_P2;
675         var1 >>= 1;
676         var1 += var3;
677         var1 >>= 18;
678         var1 += 32768;
679         var1 *= st->rom.bmp280.dig_P1;
680         var1 >>= 15;
681         if (!var1)
682                 return -1;
683
684         p = ((u32)(((s32)1048576) - adc_p) - (var2 >> 12)) * 3125;
685         if (p < 0x80000000)
686                 p = (p << 1) / ((u32)var1);
687         else
688                 p = (p / (u32)var1) << 1;
689         var3 = p >> 3;
690         var3 *= var3;
691         var3 >>= 13;
692         var1 = (s32)st->rom.bmp280.dig_P9 * var3;
693         var1 >>= 12;
694         var2 = (s32)(p >> 2);
695         var2 *= (s32)st->rom.bmp280.dig_P8;
696         var2 >>= 13;
697         var3 = var1 + var2 + st->rom.bmp280.dig_P7;
698         var3 >>= 4;
699         p = (u32)((s32)p + var3);
700         st->pressure = (int)p;
701         return 1;
702 }
703
704 static int bmp_read_sts_280(struct bmp_state *st, u8 *data, s64 ts)
705 {
706         s32 val;
707         int ret;
708
709         if (data[0] & (1 << BMP280_REG_STATUS_IM_UPDATE))
710                 /* registers are updating */
711                 return 0;
712
713         val = (data[4] << 16) | (data[5] << 8) | data[6];
714         val = le32_to_cpup(&val);
715         val >>= 4;
716         st->up = val;
717         val = (data[7] << 16) | (data[8] << 8) | data[9];
718         val = le32_to_cpup(&val);
719         val >>= 4;
720         st->ut = val;
721         bmp_calc_temp_280(st);
722         ret = bmp_calc_pres_280(st);
723         if (ret > 0)
724                 st->ts = ts;
725         return ret;
726 }
727
728 static int bmp_read_280(struct bmp_state *st)
729 {
730         s64 ts;
731         u8 data[10];
732         int ret;
733
734         ret = bmp_i2c_rd(st, BMP280_REG_STATUS, 10, data);
735         if (ret)
736                 return ret;
737
738         ts = nvs_timestamp();
739         ret = bmp_read_sts_280(st, data, ts);
740         if (ret > 0) {
741                 if (st->nvs_st[BMP_DEV_PRES])
742                         st->nvs->handler(st->nvs_st[BMP_DEV_PRES],
743                                          &st->pressure, ts);
744                 if (st->nvs_st[BMP_DEV_TEMP])
745                         st->nvs->handler(st->nvs_st[BMP_DEV_TEMP],
746                                          &st->temp, ts);
747                 bmp_i2c_wr(st, BMP_REG_CTRL, st->data_out);
748         }
749         return 0;
750 }
751
752 #if BMP_NVI_MPU_SUPPORT
753 static void bmp_mpu_handler_280(u8 *data, unsigned int len, s64 ts, void *p_val)
754 {
755         struct bmp_state *st = (struct bmp_state *)p_val;
756         unsigned int i;
757         int ret;
758
759         if (!ts) {
760                 /* no timestamp means flush done */
761                 for (i = 0; i < BMP_DEV_N; i++) {
762                         if (st->nvs_st[i])
763                                 st->nvs->handler(st->nvs_st[i], NULL, 0);
764                 }
765                 return;
766         }
767
768         if (st->enabled) {
769                 ret = bmp_read_sts_280(st, data, ts);
770                 if (ret > 0) {
771                         if (st->nvs_st[BMP_DEV_PRES])
772                                 st->nvs->handler(st->nvs_st[BMP_DEV_PRES],
773                                                  &st->pressure, ts);
774                         if (st->nvs_st[BMP_DEV_TEMP])
775                                 st->nvs->handler(st->nvs_st[BMP_DEV_TEMP],
776                                                  &st->temp, ts);
777                 }
778         }
779 }
780
781 static void bmp_mpu_handler_180(u8 *data, unsigned int len, s64 ts, void *p_val)
782 {
783         struct bmp_state *st = (struct bmp_state *)p_val;
784         unsigned int i;
785         int ret;
786
787         if (!ts) {
788                 /* no timestamp means flush done */
789                 for (i = 0; i < BMP_DEV_N; i++) {
790                         if (st->nvs_st[i])
791                                 st->nvs->handler(st->nvs_st[i], NULL, 0);
792                 }
793                 return;
794         }
795
796         if (st->enabled) {
797                 ret = bmp_read_sts_180(st, data, ts);
798                 if (ret > 0) {
799                         if (st->nvs_st[BMP_DEV_PRES])
800                                 st->nvs->handler(st->nvs_st[BMP_DEV_PRES],
801                                                  &st->pressure, ts);
802                         if (st->nvs_st[BMP_DEV_TEMP])
803                                 st->nvs->handler(st->nvs_st[BMP_DEV_TEMP],
804                                                  &st->temp, ts);
805                         nvi_mpu_data_out(st->port_id[WR], st->data_out);
806                 } else if (ret < 0) {
807                         nvi_mpu_data_out(st->port_id[WR], st->data_out);
808                 }
809         }
810 }
811 #endif /* BMP_NVI_MPU_SUPPORT */
812
813 static void bmp_work(struct work_struct *ws)
814 {
815         struct bmp_state *st = container_of((struct delayed_work *)ws,
816                                             struct bmp_state, dw);
817         unsigned int i;
818
819         for (i = 0; i < BMP_DEV_N; i++)
820                 st->nvs->nvs_mutex_lock(st->nvs_st[i]);
821         if (st->enabled) {
822                 st->hal->bmp_read(st);
823                 schedule_delayed_work(&st->dw,
824                                       usecs_to_jiffies(st->poll_delay_us));
825         }
826         for (i = 0; i < BMP_DEV_N; i++)
827                 st->nvs->nvs_mutex_unlock(st->nvs_st[i]);
828 }
829
830 static unsigned int bmp_poll_delay(struct bmp_state *st)
831 {
832         unsigned int i;
833         unsigned int delay_us = -1;
834
835         for (i = 0; i < BMP_DEV_N; i++) {
836                 if (st->enabled & (1 << i)) {
837                         if (st->delay_us[i] < delay_us)
838                                 delay_us = st->delay_us[i];
839                 }
840         }
841         if (delay_us == -1)
842                 delay_us = (BMP_POLL_DELAY_MS_DFLT * 1000);
843         return delay_us;
844 }
845
846 static int bmp_delay(struct bmp_state *st,
847                      unsigned int delay_us, int scale_user)
848 {
849         unsigned int i;
850         int ret;
851         int ret_t = 0;
852
853         if (scale_user) {
854                 i = scale_user - 1;
855         } else {
856                 for (i = (st->hal->p->scale_i_max - 1); i > 0; i--) {
857                         if (delay_us >= (st->hal->p->scale[i].delay_ms * 1000))
858                                 break;
859                 }
860         }
861         if (i != st->scale_i) {
862                 ret = 0;
863 #if BMP_NVI_MPU_SUPPORT
864                 if (st->mpu_en)
865                         ret = nvi_mpu_delay_ms(st->port_id[WR],
866                                                st->hal->p->scale[i].delay_ms);
867 #endif /* BMP_NVI_MPU_SUPPORT */
868                 if (ret < 0)
869                         ret_t |= ret;
870                 else
871                         st->scale_i = i;
872         }
873         if (delay_us < (st->hal->p->scale[st->scale_i].delay_ms * 1000))
874                 delay_us = (st->hal->p->scale[st->scale_i].delay_ms * 1000);
875         if (delay_us != st->poll_delay_us) {
876                 if (st->sts & NVS_STS_SPEW_MSG)
877                         dev_info(&st->i2c->dev, "%s: %u\n",
878                                  __func__, delay_us);
879 #if BMP_NVI_MPU_SUPPORT
880                 ret = 0;
881                 if (st->mpu_en)
882                         ret = nvi_mpu_delay_us(st->port_id[RD],
883                                                (unsigned long)delay_us);
884                 if (ret)
885                         ret_t |= ret;
886                 else
887 #endif /* BMP_NVI_MPU_SUPPORT */
888                         st->poll_delay_us = delay_us;
889         }
890         return ret_t;
891 }
892
893 static int bmp_init_hw(struct bmp_state *st)
894 {
895         u8 *p_rom8;
896         u16 *p_rom16;
897         int i;
898         int ret = 0;
899
900         st->ut = 0;
901         st->up = 0;
902         st->temp = 0;
903         st->pressure = 0;
904         p_rom8 = (u8 *)&st->rom;
905         ret = bmp_nvi_mpu_bypass_request(st);
906         if (!ret) {
907                 ret = bmp_i2c_rd(st, st->hal->rom_addr_start,
908                                  st->hal->rom_size, p_rom8);
909                 bmp_nvi_mpu_bypass_release(st);
910         }
911         if (ret)
912                 return ret;
913
914         p_rom16 = (u16 *)&st->rom;
915         for (i = 0; i < (st->hal->rom_size >> 1); i++) {
916                 if (st->hal->rom_big_endian)
917                         *p_rom16 = be16_to_cpup(p_rom16);
918                 else
919                         *p_rom16 = le16_to_cpup(p_rom16);
920                 p_rom16++;
921         }
922         st->initd = true;
923         return ret;
924 }
925
926 static int bmp_dis(struct bmp_state *st)
927 {
928         int ret = 0;
929
930         if (st->mpu_en)
931                 ret = bmp_ports_enable(st, false);
932         else
933                 cancel_delayed_work(&st->dw);
934         if (!ret)
935                 st->enabled = 0;
936         return ret;
937 }
938
939 static int bmp_disable(struct bmp_state *st, int snsr_id)
940 {
941         bool disable = true;
942         int ret = 0;
943
944         if (snsr_id >= 0) {
945                 if (st->enabled & ~(1 << snsr_id)) {
946                         st->enabled &= ~(1 << snsr_id);
947                         disable = false;
948                 }
949         }
950         if (disable) {
951                 ret = bmp_dis(st);
952                 if (!ret)
953                         bmp_pm(st, false);
954         }
955         return ret;
956 }
957
958 static int bmp_en(struct bmp_state *st)
959 {
960         int ret = 0;
961
962         bmp_pm(st, true);
963         if (!st->initd)
964                 ret = bmp_init_hw(st);
965         return ret;
966 }
967
968 static int bmp_enable(void *client, int snsr_id, int enable)
969 {
970         struct bmp_state *st = (struct bmp_state *)client;
971         int ret;
972
973         if (enable < 0)
974                 return st->enabled & (1 << snsr_id);
975
976         if (enable) {
977                 enable = st->enabled | (1 << snsr_id);
978                 ret = bmp_en(st);
979                 if (!ret) {
980                         ret |= bmp_delay(st, bmp_poll_delay(st),
981                                          st->scale_user);
982                         ret |= bmp_mode(st);
983                         if (st->mpu_en)
984                                 ret |= bmp_ports_enable(st, true);
985                         if (ret) {
986                                 bmp_disable(st, snsr_id);
987                         } else {
988                                 st->enabled = enable;
989                                 if (!st->mpu_en)
990                                         schedule_delayed_work(&st->dw,
991                                                               usecs_to_jiffies(
992                                                            st->poll_delay_us));
993                         }
994                 }
995         } else {
996                 ret = bmp_disable(st, snsr_id);
997         }
998         return ret;
999 }
1000
1001 static int bmp_batch(void *client, int snsr_id, int flags,
1002                      unsigned int period, unsigned int timeout)
1003 {
1004         struct bmp_state *st = (struct bmp_state *)client;
1005         unsigned int old;
1006         int ret = 0;
1007
1008 #if BMP_NVI_MPU_SUPPORT
1009         if (st->mpu_en)
1010                 ret = nvi_mpu_batch(st->port_id[RD], flags, period, timeout);
1011         else
1012 #endif /* BMP_NVI_MPU_SUPPORT */
1013                 if (timeout)
1014                         /* timeout not supported (no HW FIFO) */
1015                         return -EINVAL;
1016
1017         old = st->delay_us[snsr_id];
1018         st->delay_us[snsr_id] = period;
1019         ret |= bmp_delay(st, bmp_poll_delay(st), st->scale_user);
1020         if (st->enabled && st->i2c->irq && !ret)
1021                 ret = bmp_mode(st);
1022         if (ret)
1023                 st->delay_us[snsr_id] = old;
1024         return 0;
1025 }
1026
1027 static int bmp_flush(void *client, int snsr_id)
1028 {
1029         int ret = -EINVAL;
1030 #if BMP_NVI_MPU_SUPPORT
1031         struct bmp_state *st = (struct bmp_state *)client;
1032
1033         if (st->mpu_en)
1034                 ret = nvi_mpu_flush(st->port_id[RD]);
1035 #endif /* BMP_NVI_MPU_SUPPORT */
1036         return ret;
1037 }
1038
1039 static int bmp_resolution(void *client, int snsr_id, int resolution)
1040 {
1041         struct bmp_state *st = (struct bmp_state *)client;
1042         int ret;
1043
1044         if (snsr_id != BMP_DEV_PRES)
1045                 return -EINVAL;
1046
1047         if (resolution < 0 || resolution > st->hal->p->scale_i_max)
1048                 return -EINVAL;
1049
1050         ret = bmp_delay(st, st->poll_delay_us, resolution);
1051         if (!ret) {
1052                 st->scale_user = resolution;
1053                 if (st->enabled && st->i2c->irq)
1054                         ret = bmp_mode(st);
1055         }
1056         if (ret)
1057                 return ret;
1058
1059         st->cfg[BMP_DEV_PRES].resolution.ival =
1060                                 st->hal->p->scale[st->scale_i].resolution.ival;
1061         st->cfg[BMP_DEV_PRES].resolution.fval =
1062                                 st->hal->p->scale[st->scale_i].resolution.fval;
1063         st->cfg[BMP_DEV_PRES].milliamp.ival =
1064                                   st->hal->p->scale[st->scale_i].milliamp.ival;
1065         st->cfg[BMP_DEV_PRES].milliamp.fval =
1066                                   st->hal->p->scale[st->scale_i].milliamp.fval;
1067         st->cfg[BMP_DEV_PRES].delay_us_min =
1068                                 st->hal->p->scale[st->scale_i].delay_ms * 1000;
1069         st->cfg[BMP_DEV_TEMP].resolution.ival =
1070                                 st->hal->t->scale[st->scale_i].resolution.ival;
1071         st->cfg[BMP_DEV_TEMP].resolution.fval =
1072                                 st->hal->t->scale[st->scale_i].resolution.fval;
1073         st->cfg[BMP_DEV_TEMP].milliamp.ival =
1074                                   st->hal->t->scale[st->scale_i].milliamp.ival;
1075         st->cfg[BMP_DEV_TEMP].milliamp.fval =
1076                                   st->hal->t->scale[st->scale_i].milliamp.fval;
1077         st->cfg[BMP_DEV_TEMP].delay_us_min =
1078                                 st->hal->t->scale[st->scale_i].delay_ms * 1000;
1079         return 0;
1080 }
1081
1082 static int bmp_reset(void *client, int snsr_id)
1083 {
1084         struct bmp_state *st = (struct bmp_state *)client;
1085         unsigned int enabled = st->enabled;
1086         unsigned int i;
1087         int ret;
1088
1089         bmp_dis(st);
1090         bmp_en(st);
1091         ret = bmp_wr(st, BMP_REG_RESET, BMP_REG_RESET_VAL);
1092         if (!ret)
1093                 mdelay(BMP_HW_DELAY_POR_MS);
1094         for (i = 0; i < BMP_DEV_N; i++)
1095                 bmp_enable(st, i, enabled & (1 << i));
1096         return ret;
1097 }
1098
1099 static int bmp_regs(void *client, int snsr_id, char *buf)
1100 {
1101         struct bmp_state *st = (struct bmp_state *)client;
1102         ssize_t t;
1103         u8 data[11];
1104         u16 *cal;
1105         unsigned int i;
1106         int ret;
1107
1108         if (!st->initd) {
1109                 t = sprintf(buf, "calibration: NEED ENABLE\n");
1110         } else {
1111                 t = sprintf(buf, "calibration:\n");
1112                 cal = &st->rom.bmp280.dig_T1;
1113                 for (i = 0; i < st->hal->rom_size; i = i + 2)
1114                         t += sprintf(buf + t, "%#2x=%#2x\n",
1115                                      st->hal->rom_addr_start + i,
1116                                      *cal++);
1117         }
1118         ret = bmp_nvi_mpu_bypass_request(st);
1119         if (!ret) {
1120                 ret = bmp_i2c_rd(st, BMP_REG_ID, 1, data);
1121                 ret |= bmp_i2c_rd(st, BMP280_REG_STATUS,
1122                                   10, &data[1]);
1123                 bmp_nvi_mpu_bypass_release(st);
1124         }
1125         if (ret) {
1126                 t += sprintf(buf + t, "registers: ERR %d\n", ret);
1127         } else {
1128                 t += sprintf(buf + t, "registers:\n");
1129                 t += sprintf(buf + t, "%#2x=%#2x\n",
1130                              BMP_REG_ID, data[0]);
1131                 for (i = 0; i < 10; i++)
1132                         t += sprintf(buf + t, "%#2x=%#2x\n",
1133                                      BMP280_REG_STATUS + i,
1134                                      data[i + 1]);
1135         }
1136         return t;
1137 }
1138
1139 static struct nvs_fn_dev bmp_fn_dev = {
1140         .enable                         = bmp_enable,
1141         .batch                          = bmp_batch,
1142         .flush                          = bmp_flush,
1143         .resolution                     = bmp_resolution,
1144         .reset                          = bmp_reset,
1145         .regs                           = bmp_regs,
1146 };
1147
1148 static int bmp_suspend(struct device *dev)
1149 {
1150         struct i2c_client *client = to_i2c_client(dev);
1151         struct bmp_state *st = i2c_get_clientdata(client);
1152         unsigned int i;
1153         int ret = 0;
1154
1155         st->sts |= NVS_STS_SUSPEND;
1156         for (i = 0; i < BMP_DEV_N; i++) {
1157                 if (st->nvs && st->nvs_st[i])
1158                         ret |= st->nvs->suspend(st->nvs_st[i]);
1159         }
1160         if (st->sts & NVS_STS_SPEW_MSG)
1161                 dev_info(&client->dev, "%s\n", __func__);
1162         return ret;
1163 }
1164
1165 static int bmp_resume(struct device *dev)
1166 {
1167         struct i2c_client *client = to_i2c_client(dev);
1168         struct bmp_state *st = i2c_get_clientdata(client);
1169         unsigned int i;
1170         int ret = 0;
1171
1172         for (i = 0; i < BMP_DEV_N; i++) {
1173                 if (st->nvs && st->nvs_st[i])
1174                         ret |= st->nvs->resume(st->nvs_st[i]);
1175         }
1176         st->sts &= ~NVS_STS_SUSPEND;
1177         if (st->sts & NVS_STS_SPEW_MSG)
1178                 dev_info(&client->dev, "%s\n", __func__);
1179         return ret;
1180 }
1181
1182 static SIMPLE_DEV_PM_OPS(bmp_pm_ops, bmp_suspend, bmp_resume);
1183
1184 static void bmp_shutdown(struct i2c_client *client)
1185 {
1186         struct bmp_state *st = i2c_get_clientdata(client);
1187         unsigned int i;
1188
1189         st->sts |= NVS_STS_SHUTDOWN;
1190         for (i = 0; i < BMP_DEV_N; i++) {
1191                 if (st->nvs && st->nvs_st[i])
1192                         st->nvs->shutdown(st->nvs_st[i]);
1193         }
1194         if (st->sts & NVS_STS_SPEW_MSG)
1195                 dev_info(&client->dev, "%s\n", __func__);
1196 }
1197
1198 static int bmp_remove(struct i2c_client *client)
1199 {
1200         struct bmp_state *st = i2c_get_clientdata(client);
1201         unsigned int i;
1202
1203         if (st != NULL) {
1204                 bmp_shutdown(client);
1205                 if (st->nvs) {
1206                         for (i = 0; i < BMP_DEV_N; i++) {
1207                                 if (st->nvs_st[i]) {
1208                                         st->nvs->remove(st->nvs_st[i]);
1209                                         st->nvs_st[i] = NULL;
1210                                 }
1211                         }
1212                 }
1213                 if (st->dw.wq)
1214                         destroy_workqueue(st->dw.wq);
1215                 bmp_pm_exit(st);
1216         }
1217         dev_info(&client->dev, "%s\n", __func__);
1218         return 0;
1219 }
1220
1221 struct sensor_cfg bmp_cfg_dflt[] = {
1222         {
1223                 .name                   = "pressure",
1224                 .snsr_id                = BMP_DEV_PRES,
1225                 .kbuf_sz                = BMP_KBUF_SIZE,
1226                 .ch_n                   = 1,
1227                 .ch_sz                  = -4,
1228                 .part                   = BMP_NAME,
1229                 .vendor                 = BMP_VENDOR,
1230                 .version                = 0,
1231                 .max_range              = {
1232                         .ival           = BMP_PRES_MAX_RANGE_IVAL,
1233                         .fval           = BMP_PRES_MAX_RANGE_MICRO,
1234                 },
1235                 .delay_us_min           = 10000,
1236                 .delay_us_max           = 255000,
1237         },
1238         {
1239                 .name                   = "ambient_temperature",
1240                 .snsr_id                = BMP_DEV_TEMP,
1241                 .ch_n                   = 1,
1242                 .ch_sz                  = -4,
1243                 .part                   = BMP_NAME,
1244                 .vendor                 = BMP_VENDOR,
1245                 .version                = 0,
1246                 .max_range              = {
1247                         .ival           = BMP_TEMP_MAX_RANGE_IVAL,
1248                         .fval           = BMP_TEMP_MAX_RANGE_MICRO,
1249                 },
1250                 .delay_us_min           = 10000,
1251                 .delay_us_max           = 255000,
1252                 .flags                  = SENSOR_FLAG_ON_CHANGE_MODE,
1253         },
1254 };
1255
1256 static struct bmp_scale bmp_scale_pres_180[] = {
1257         {
1258                 .delay_ms               = 5,
1259                 .resolution             = {
1260                         .ival           = 0,
1261                         .fval           = 10000,
1262                 },
1263                 .milliamp               = {
1264                         .ival           = 0,
1265                         .fval           = 3000,
1266                 },
1267         },
1268         {
1269                 .delay_ms               = 8,
1270                 .resolution             = {
1271                         .ival           = 0,
1272                         .fval           = 10000,
1273                 },
1274                 .milliamp               = {
1275                         .ival           = 0,
1276                         .fval           = 5000,
1277                 },
1278         },
1279         {
1280                 .delay_ms               = 14,
1281                 .resolution             = {
1282                         .ival           = 0,
1283                         .fval           = 10000,
1284                 },
1285                 .milliamp               = {
1286                         .ival           = 0,
1287                         .fval           = 7000,
1288                 },
1289         },
1290         {
1291                 .delay_ms               = 26,
1292                 .resolution             = {
1293                         .ival           = 0,
1294                         .fval           = 10000,
1295                 },
1296                 .milliamp               = {
1297                         .ival           = 0,
1298                         .fval           = 12000,
1299                 },
1300         },
1301 };
1302
1303 static struct bmp_scale bmp_scale_temp_180[] = {
1304         {
1305                 .delay_ms               = 5,
1306                 .resolution             = {
1307                         .ival           = 0,
1308                         .fval           = 100000,
1309                 },
1310                 .milliamp               = {
1311                         .ival           = 0,
1312                         .fval           = 3000,
1313                 },
1314         },
1315         {
1316                 .delay_ms               = 8,
1317                 .resolution             = {
1318                         .ival           = 0,
1319                         .fval           = 100000,
1320                 },
1321                 .milliamp               = {
1322                         .ival           = 0,
1323                         .fval           = 5000,
1324                 },
1325         },
1326         {
1327                 .delay_ms               = 14,
1328                 .resolution             = {
1329                         .ival           = 0,
1330                         .fval           = 100000,
1331                 },
1332                 .milliamp               = {
1333                         .ival           = 0,
1334                         .fval           = 7000,
1335                 },
1336         },
1337         {
1338                 .delay_ms               = 26,
1339                 .resolution             = {
1340                         .ival           = 0,
1341                         .fval           = 100000,
1342                 },
1343                 .milliamp               = {
1344                         .ival           = 0,
1345                         .fval           = 12000,
1346                 },
1347         },
1348 };
1349
1350 static struct bmp_hal_dev bmp_hal_dev_pres_180 = {
1351         .version                        = 1,
1352         .scale_i_max                    = ARRAY_SIZE(bmp_scale_pres_180),
1353         .scale_dflt                     = BMP180_RANGE_DFLT,
1354         .scale                          = bmp_scale_pres_180,
1355         .scale_float                    = {
1356                 .ival                   = 0,
1357                 .fval                   = 10000,
1358         }
1359 };
1360
1361 static struct bmp_hal_dev bmp_hal_dev_temp_180 = {
1362         .version                        = 1,
1363         .scale_i_max                    = ARRAY_SIZE(bmp_scale_temp_180),
1364         .scale_dflt                     = BMP180_RANGE_DFLT,
1365         .scale                          = bmp_scale_temp_180,
1366         .scale_float                    = {
1367                 .ival                   = 0,
1368                 .fval                   = 100000,
1369         }
1370 };
1371
1372 static struct bmp_hal bmp_hal_180 = {
1373         .p                              = &bmp_hal_dev_pres_180,
1374         .t                              = &bmp_hal_dev_temp_180,
1375         .part                           = BMP180_NAME,
1376         .rom_addr_start                 = BMP180_REG_AC1,
1377         .rom_size                       = 22,
1378         .rom_big_endian                 = true,
1379         .mode_mask                      = BMP180_REG_CTRL_MODE_MASK,
1380         .cmode_tbl                      = NULL,
1381         .bmp_read                       = &bmp_read_180,
1382 };
1383
1384 static struct bmp_scale bmp_scale_pres_280[] = {
1385         {
1386                 .delay_ms               = 9,
1387                 .resolution             = {
1388                         .ival           = 0,
1389                         .fval           = 28700,
1390                 },
1391                 .milliamp               = {
1392                         .ival           = 0,
1393                         .fval           = 2740,
1394                 },
1395         },
1396         {
1397                 .delay_ms               = 12,
1398                 .resolution             = {
1399                         .ival           = 0,
1400                         .fval           = 14300,
1401                 },
1402                 .milliamp               = {
1403                         .ival           = 0,
1404                         .fval           = 4170,
1405                 },
1406         },
1407         {
1408                 .delay_ms               = 18,
1409                 .resolution             = {
1410                         .ival           = 0,
1411                         .fval           = 7200,
1412                 },
1413                 .milliamp               = {
1414                         .ival           = 0,
1415                         .fval           = 7020,
1416                 },
1417         },
1418         {
1419                 .delay_ms               = 30,
1420                 .resolution             = {
1421                         .ival           = 0,
1422                         .fval           = 3600,
1423                 },
1424                 .milliamp               = {
1425                         .ival           = 0,
1426                         .fval           = 12700,
1427                 },
1428         },
1429         {
1430                 .delay_ms               = 57,
1431                 .resolution             = {
1432                         .ival           = 0,
1433                         .fval           = 1800,
1434                 },
1435                 .milliamp               = {
1436                         .ival           = 0,
1437                         .fval           = 24800,
1438                 },
1439         },
1440 };
1441
1442 static struct bmp_scale bmp_scale_temp_280[] = {
1443         {
1444                 .delay_ms               = 9,
1445                 .resolution             = {
1446                         .ival           = 0,
1447                         .fval           = 5000,
1448                 },
1449                 .milliamp               = {
1450                         .ival           = 0,
1451                         .fval           = 2740,
1452                 },
1453         },
1454         {
1455                 .delay_ms               = 12,
1456                 .resolution             = {
1457                         .ival           = 0,
1458                         .fval           = 2500,
1459                 },
1460                 .milliamp               = {
1461                         .ival           = 0,
1462                         .fval           = 4170,
1463                 },
1464         },
1465         {
1466                 .delay_ms               = 18,
1467                 .resolution             = {
1468                         .ival           = 0,
1469                         .fval           = 1200,
1470                 },
1471                 .milliamp               = {
1472                         .ival           = 0,
1473                         .fval           = 7020,
1474                 },
1475         },
1476         {
1477                 .delay_ms               = 30,
1478                 .resolution             = {
1479                         .ival           = 0,
1480                         .fval           = 600,
1481                 },
1482                 .milliamp               = {
1483                         .ival           = 0,
1484                         .fval           = 12700,
1485                 },
1486         },
1487         {
1488                 .delay_ms               = 57,
1489                 .resolution             = {
1490                         .ival           = 0,
1491                         .fval           = 300,
1492                 },
1493                 .milliamp               = {
1494                         .ival           = 0,
1495                         .fval           = 24800,
1496                 },
1497         },
1498 };
1499
1500 static struct bmp_hal_dev bmp_hal_dev_pres_280 = {
1501         .version                        = 1,
1502         .scale_i_max                    = ARRAY_SIZE(bmp_scale_pres_280),
1503         .scale_dflt                     = BMP280_RANGE_DFLT,
1504         .scale                          = bmp_scale_pres_280,
1505         .scale_float                    = {
1506                 .ival                   = 0,
1507                 .fval                   = 10000,
1508         }
1509 };
1510
1511 static struct bmp_hal_dev bmp_hal_dev_temp_280 = {
1512         .version                        = 1,
1513         .scale_i_max                    = ARRAY_SIZE(bmp_scale_temp_280),
1514         .scale_dflt                     = BMP280_RANGE_DFLT,
1515         .scale                          = bmp_scale_temp_280,
1516         .scale_float                    = {
1517                 .ival                   = 0,
1518                 .fval                   = 10000,
1519         }
1520 };
1521
1522 static struct bmp_cmode bmp_cmode_280[] = {
1523         {
1524                 .t_us                   = 4000000,
1525                 .t_sb                   = 0x07,
1526         },
1527         {
1528                 .t_us                   = 2000000,
1529                 .t_sb                   = 0x06,
1530         },
1531         {
1532                 .t_us                   = 1000000,
1533                 .t_sb                   = 0x05,
1534         },
1535         {
1536                 .t_us                   = 500000,
1537                 .t_sb                   = 0x04,
1538         },
1539         {
1540                 .t_us                   = 250000,
1541                 .t_sb                   = 0x03,
1542         },
1543         {
1544                 .t_us                   = 125000,
1545                 .t_sb                   = 0x02,
1546         },
1547         {
1548                 .t_us                   = 62500,
1549                 .t_sb                   = 0x01,
1550         },
1551         {
1552                 .t_us                   = 500,
1553                 .t_sb                   = 0x00,
1554         },
1555         {},
1556 };
1557
1558 static struct bmp_hal bmp_hal_280 = {
1559         .p                              = &bmp_hal_dev_pres_280,
1560         .t                              = &bmp_hal_dev_temp_280,
1561         .part                           = BMP280_NAME,
1562         .rom_addr_start                 = BMP280_REG_CWORD00,
1563         .rom_size                       = 26,
1564         .rom_big_endian                 = false,
1565         .mode_mask                      = BMP280_REG_CTRL_MODE_MASK,
1566         .cmode_tbl                      = bmp_cmode_280,
1567         .bmp_read                       = &bmp_read_280,
1568 #if BMP_NVI_MPU_SUPPORT
1569         .mpu_id                         = PRESSURE_ID_BMP280,
1570 #endif /* BMP_NVI_MPU_SUPPORT */
1571 };
1572
1573 static int bmp_id_hal(struct bmp_state *st)
1574 {
1575         unsigned int i;
1576         int ret = 0;
1577
1578         switch (st->dev_id) {
1579         case BMP_REG_ID_BMP280:
1580                 st->hal = &bmp_hal_280;
1581                 break;
1582
1583         case BMP_REG_ID_BMP180:
1584                 st->hal = &bmp_hal_180;
1585                 break;
1586
1587         default:
1588                 dev_err(&st->i2c->dev, "%s ERR: Unknown device\n", __func__);
1589                 st->hal = &bmp_hal_180; /* to prevent NULL pointers */
1590                 ret = -ENODEV;
1591                 break;
1592         }
1593
1594         st->scale_user = st->hal->p->scale_dflt;
1595         for (i = 0; i < BMP_DEV_N; i++)
1596                 memcpy(&st->cfg[i], &bmp_cfg_dflt[i],
1597                        sizeof(struct sensor_cfg));
1598         st->cfg[BMP_DEV_PRES].part = st->hal->part;
1599         st->cfg[BMP_DEV_PRES].version = st->hal->p->version;
1600         st->cfg[BMP_DEV_PRES].resolution.ival =
1601                                 st->hal->p->scale[st->scale_i].resolution.ival;
1602         st->cfg[BMP_DEV_PRES].resolution.fval =
1603                                 st->hal->p->scale[st->scale_i].resolution.fval;
1604         st->cfg[BMP_DEV_PRES].milliamp.ival =
1605                                   st->hal->p->scale[st->scale_i].milliamp.ival;
1606         st->cfg[BMP_DEV_PRES].milliamp.fval =
1607                                   st->hal->p->scale[st->scale_i].milliamp.fval;
1608         st->cfg[BMP_DEV_PRES].delay_us_min =
1609                                 st->hal->p->scale[st->scale_i].delay_ms * 1000;
1610         st->cfg[BMP_DEV_PRES].scale.ival = st->hal->p->scale_float.ival;
1611         st->cfg[BMP_DEV_PRES].scale.fval = st->hal->p->scale_float.fval;
1612         st->cfg[BMP_DEV_TEMP].part = st->hal->part;
1613         st->cfg[BMP_DEV_TEMP].version = st->hal->t->version;
1614         st->cfg[BMP_DEV_TEMP].resolution.ival =
1615                                 st->hal->t->scale[st->scale_i].resolution.ival;
1616         st->cfg[BMP_DEV_TEMP].resolution.fval =
1617                                 st->hal->t->scale[st->scale_i].resolution.fval;
1618         st->cfg[BMP_DEV_TEMP].milliamp.ival =
1619                                   st->hal->t->scale[st->scale_i].milliamp.ival;
1620         st->cfg[BMP_DEV_TEMP].milliamp.fval =
1621                                   st->hal->t->scale[st->scale_i].milliamp.fval;
1622         st->cfg[BMP_DEV_TEMP].delay_us_min =
1623                                 st->hal->t->scale[st->scale_i].delay_ms * 1000;
1624         st->cfg[BMP_DEV_TEMP].scale.ival = st->hal->t->scale_float.ival;
1625         st->cfg[BMP_DEV_TEMP].scale.fval = st->hal->t->scale_float.fval;
1626         return ret;
1627 }
1628
1629 static int bmp_id_compare(struct bmp_state *st, u8 val, const char *name)
1630 {
1631         unsigned int i;
1632         int ret = 0;
1633
1634         for (i = 0; i < ARRAY_SIZE(bmp_ids); i++) {
1635                 if (val == bmp_ids[i]) {
1636                         if ((st->dev_id == BMP_REG_ID_BMP180) &&
1637                                                            (st->dev_id != val))
1638                                 dev_err(&st->i2c->dev, "%s ERR: %x != %s\n",
1639                                         __func__, st->dev_id, name);
1640                         if (val != BMP_REG_ID_BMP180)
1641                                 /* BMP280 may have more ID's than 0x56 */
1642                                 val = BMP_REG_ID_BMP280;
1643                         st->dev_id = val;
1644                         break;
1645                 }
1646         }
1647         if (!st->dev_id) {
1648                 ret = -ENODEV;
1649                 dev_err(&st->i2c->dev, "%s ERR: ID %x != %s\n",
1650                         __func__, val, name);
1651         } else {
1652                 dev_dbg(&st->i2c->dev, "%s using ID %x for %s\n",
1653                         __func__, st->dev_id, name);
1654         }
1655         return ret;
1656 }
1657
1658 static int bmp_id_dev(struct bmp_state *st, const char *name)
1659 {
1660 #if BMP_NVI_MPU_SUPPORT
1661         struct nvi_mpu_port nmp;
1662         u8 config_boot;
1663 #endif /* BMP_NVI_MPU_SUPPORT */
1664         u8 val = 0;
1665         int ret;
1666
1667         if (!strcmp(name, BMP180_NAME))
1668                 st->dev_id = BMP_REG_ID_BMP180;
1669         else if (!strcmp(name, BMP280_NAME))
1670                 st->dev_id = BMP_REG_ID_BMP280;
1671 #if BMP_NVI_MPU_SUPPORT
1672         config_boot = st->nvi_config & NVI_CONFIG_BOOT_MASK;
1673         if ((config_boot == NVI_CONFIG_BOOT_MPU) && (!st->dev_id)) {
1674                 dev_err(&st->i2c->dev, "%s ERR: NVI_CONFIG_BOOT_MPU && %s\n",
1675                         __func__, name);
1676                 config_boot = NVI_CONFIG_BOOT_AUTO;
1677         }
1678         if (config_boot == NVI_CONFIG_BOOT_AUTO) {
1679                 nmp.addr = st->i2c_addr | 0x80;
1680                 nmp.reg = BMP_REG_ID;
1681                 nmp.ctrl = 1;
1682                 ret = nvi_mpu_dev_valid(&nmp, &val);
1683                 dev_info(&st->i2c->dev, "%s AUTO ID=%x ret=%d\n",
1684                          __func__, val, ret);
1685                 /* see mpu_iio.h for possible return values */
1686                 if ((ret == -EAGAIN) || (ret == -EBUSY))
1687                         return -EAGAIN;
1688
1689                 if (!ret)
1690                         ret = bmp_id_compare(st, val, name);
1691                 if ((!ret) || ((ret == -EIO) && (st->dev_id)))
1692                         config_boot = NVI_CONFIG_BOOT_MPU;
1693         }
1694         if (config_boot == NVI_CONFIG_BOOT_MPU) {
1695                 st->mpu_en = true;
1696                 bmp_id_hal(st);
1697                 nmp.addr = st->i2c_addr | 0x80;
1698                 nmp.data_out = 0;
1699                 nmp.delay_ms = 0;
1700                 nmp.delay_us = st->poll_delay_us;
1701                 if ((st->hal->cmode_tbl != NULL) && st->i2c->irq)
1702                         nmp.shutdown_bypass = true;
1703                 else
1704                         nmp.shutdown_bypass = false;
1705                 nmp.ext_driver = (void *)st;
1706                 if (st->dev_id == BMP_REG_ID_BMP180) {
1707                         nmp.reg = BMP_REG_CTRL;
1708                         nmp.ctrl = 6; /* MPU FIFO can't handle odd size */
1709                         nmp.handler = &bmp_mpu_handler_180;
1710                 } else {
1711                         nmp.reg = BMP280_REG_STATUS;
1712                         nmp.ctrl = 10; /* MPU FIFO can't handle odd size */
1713                         nmp.handler = &bmp_mpu_handler_280;
1714                 }
1715                 nmp.rate_scale = 34;
1716                 ret = nvi_mpu_port_alloc(&nmp);
1717                 dev_dbg(&st->i2c->dev, "%s MPU port/ret=%d\n",
1718                         __func__, ret);
1719                 if (ret < 0)
1720                         return ret;
1721
1722                 st->port_id[RD] = ret;
1723                 ret = 0;
1724                 if ((st->hal->cmode_tbl == NULL) || !st->i2c->irq) {
1725                         st->i2c->irq = 0;
1726                         nmp.addr = st->i2c_addr;
1727                         nmp.reg = BMP_REG_CTRL;
1728                         nmp.ctrl = 1;
1729                         nmp.data_out = st->data_out;
1730                         nmp.delay_ms = st->hal->p->scale[st->scale_i].delay_ms;
1731                         nmp.delay_us = 0;
1732                         nmp.shutdown_bypass = false;
1733                         nmp.handler = NULL;
1734                         nmp.ext_driver = NULL;
1735                         nmp.type = SECONDARY_SLAVE_TYPE_PRESSURE;
1736                         nmp.id = st->hal->mpu_id;
1737                         ret = nvi_mpu_port_alloc(&nmp);
1738                         dev_dbg(&st->i2c->dev, "%s MPU port/ret=%d\n",
1739                                 __func__, ret);
1740                         if (ret < 0) {
1741                                 bmp_ports_free(st);
1742                                 dev_err(&st->i2c->dev, "%s ERR %d\n",
1743                                         __func__, ret);
1744                         } else {
1745                                 st->port_id[WR] = ret;
1746                                 ret = 0;
1747                         }
1748                 }
1749                 nvi_mpu_fifo(st->port_id[RD],
1750                              &st->cfg[BMP_DEV_PRES].fifo_rsrv_evnt_cnt,
1751                              &st->cfg[BMP_DEV_PRES].fifo_max_evnt_cnt);
1752                 st->cfg[BMP_DEV_TEMP].fifo_rsrv_evnt_cnt =
1753                                       st->cfg[BMP_DEV_PRES].fifo_rsrv_evnt_cnt;
1754                 st->cfg[BMP_DEV_TEMP].fifo_max_evnt_cnt =
1755                                        st->cfg[BMP_DEV_PRES].fifo_max_evnt_cnt;
1756                 return ret;
1757         }
1758 #endif /* BMP_NVI_MPU_SUPPORT */
1759         /* NVI_CONFIG_BOOT_HOST */
1760         st->mpu_en = false;
1761         ret = bmp_i2c_rd(st, BMP_REG_ID, 1, &val);
1762         dev_dbg(&st->i2c->dev, "%s Host read ID=%x ret=%d\n",
1763                 __func__, val, ret);
1764         if (!ret) {
1765                 ret = bmp_id_compare(st, val, name);
1766                 if (!ret)
1767                         ret = bmp_id_hal(st);
1768         }
1769         return ret;
1770 }
1771
1772 static int bmp_id_i2c(struct bmp_state *st,
1773                       const struct i2c_device_id *id)
1774 {
1775         int i;
1776         int ret;
1777
1778         for (i = 0; i < ARRAY_SIZE(bmp_i2c_addrs); i++) {
1779                 if (st->i2c->addr == bmp_i2c_addrs[i])
1780                         break;
1781         }
1782
1783         if (i < ARRAY_SIZE(bmp_i2c_addrs)) {
1784                 st->i2c_addr = st->i2c->addr;
1785                 ret = bmp_id_dev(st, id->name);
1786         } else {
1787                 for (i = 0; i < ARRAY_SIZE(bmp_i2c_addrs); i++) {
1788                         st->i2c_addr = bmp_i2c_addrs[i];
1789                         ret = bmp_id_dev(st, BMP_NAME);
1790                         if ((ret == -EAGAIN) || (!ret))
1791                                 break;
1792                 }
1793         }
1794         if (ret)
1795                 st->i2c_addr = 0;
1796         return ret;
1797 }
1798
1799 static int bmp_of_dt(struct bmp_state *st, struct device_node *dn)
1800 {
1801         char const *pchar;
1802         u8 cfg;
1803         int ret;
1804
1805         if (dn) {
1806                 /* just test if global disable */
1807                 ret = nvs_of_dt(dn, NULL, NULL);
1808                 if (ret == -ENODEV)
1809                         return -ENODEV;
1810
1811                 /* this device supports these programmable parameters */
1812                 if (!(of_property_read_string(dn, "nvi_config", &pchar))) {
1813                         for (cfg = 0; cfg < ARRAY_SIZE(bmp_configs); cfg++) {
1814                                 if (!strcasecmp(pchar, bmp_configs[cfg])) {
1815                                         st->nvi_config = cfg;
1816                                         break;
1817                                 }
1818                         }
1819                 }
1820         }
1821
1822         return 0;
1823 }
1824
1825 static int bmp_probe(struct i2c_client *client,
1826                      const struct i2c_device_id *id)
1827 {
1828         struct bmp_state *st;
1829         unsigned int i;
1830         int ret;
1831
1832         dev_info(&client->dev, "%s %s\n", id->name, __func__);
1833         st = devm_kzalloc(&client->dev, sizeof(*st), GFP_KERNEL);
1834         if (st == NULL) {
1835                 dev_err(&client->dev, "%s devm_kzalloc ERR\n", __func__);
1836                 return -ENOMEM;
1837         }
1838
1839         i2c_set_clientdata(client, st);
1840         st->i2c = client;
1841         ret = bmp_of_dt(st, client->dev.of_node);
1842         if (ret) {
1843                 if (ret == -ENODEV) {
1844                         dev_info(&client->dev, "%s DT disabled\n", __func__);
1845                 } else {
1846                         dev_err(&client->dev, "%s _of_dt ERR\n", __func__);
1847                         ret = -ENODEV;
1848                 }
1849                 goto bmp_probe_err;
1850         }
1851
1852         bmp_pm_init(st);
1853         ret = bmp_id_i2c(st, id);
1854         if (ret == -EAGAIN)
1855                 goto bmp_probe_again;
1856         else if (ret)
1857                 goto bmp_probe_err;
1858
1859         bmp_init_hw(st);
1860         bmp_pm(st, false);
1861         bmp_fn_dev.errs = &st->errs;
1862         bmp_fn_dev.sts = &st->sts;
1863         st->nvs = nvs_iio();
1864         if (st->nvs == NULL) {
1865                 ret = -ENODEV;
1866                 goto bmp_probe_err;
1867         }
1868
1869         for (i = 0; i < BMP_DEV_N; i++) {
1870                 nvs_of_dt(client->dev.of_node, &st->cfg[i], NULL);
1871                 ret |= st->nvs->probe(&st->nvs_st[i], st, &client->dev,
1872                                       &bmp_fn_dev, &st->cfg[i]);
1873         }
1874         if (ret) {
1875                 dev_err(&client->dev, "%s nvs_probe ERR\n", __func__);
1876                 ret = -ENODEV;
1877                 goto bmp_probe_err;
1878         }
1879
1880         if (!st->mpu_en)
1881                 INIT_DELAYED_WORK(&st->dw, bmp_work);
1882
1883         dev_info(&client->dev, "%s done\n", __func__);
1884         return 0;
1885
1886 bmp_probe_err:
1887         dev_err(&client->dev, "%s ERR %d\n", __func__, ret);
1888 bmp_probe_again:
1889         bmp_remove(client);
1890         return ret;
1891 }
1892
1893 static const struct i2c_device_id bmp_i2c_device_id[] = {
1894         { BMP_NAME, 0 },
1895         { BMP180_NAME, 0 },
1896         { BMP280_NAME, 0 },
1897         {}
1898 };
1899
1900 MODULE_DEVICE_TABLE(i2c, bmp_i2c_device_id);
1901
1902 static const struct of_device_id bmp_of_match[] = {
1903         { .compatible = "bmp,bmpX80", },
1904         { .compatible = "bmp,bmp180", },
1905         { .compatible = "bmp,bmp280", },
1906         {},
1907 };
1908
1909 MODULE_DEVICE_TABLE(of, bmp_of_match);
1910
1911 static struct i2c_driver bmp_driver = {
1912         .class                          = I2C_CLASS_HWMON,
1913         .probe                          = bmp_probe,
1914         .remove                         = bmp_remove,
1915         .shutdown                       = bmp_shutdown,
1916         .driver                         = {
1917                 .name                   = BMP_NAME,
1918                 .owner                  = THIS_MODULE,
1919                 .of_match_table         = of_match_ptr(bmp_of_match),
1920                 .pm                     = &bmp_pm_ops,
1921         },
1922         .id_table                       = bmp_i2c_device_id,
1923 };
1924
1925 static int __init bmp_init(void)
1926 {
1927         return i2c_add_driver(&bmp_driver);
1928 }
1929
1930 static void __exit bmp_exit(void)
1931 {
1932         i2c_del_driver(&bmp_driver);
1933 }
1934
1935 late_initcall(bmp_init);
1936 module_exit(bmp_exit);
1937
1938 MODULE_LICENSE("GPL");
1939 MODULE_DESCRIPTION("BMPX80 driver");
1940 MODULE_AUTHOR("NVIDIA Corporation");
1941