root/drivers/iio/pressure/abp060mg.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. abp060mg_get_measurement
  2. abp060mg_read_raw
  3. abp060mg_init_device
  4. abp060mg_probe

   1 // SPDX-License-Identifier: GPL-2.0-or-later
   2 /*
   3  * Copyright (C) 2016 - Marcin Malagowski <mrc@bourne.st>
   4  */
   5 #include <linux/delay.h>
   6 #include <linux/device.h>
   7 #include <linux/err.h>
   8 #include <linux/i2c.h>
   9 #include <linux/io.h>
  10 #include <linux/kernel.h>
  11 #include <linux/module.h>
  12 #include <linux/iio/iio.h>
  13 
  14 #define ABP060MG_ERROR_MASK   0xC000
  15 #define ABP060MG_RESP_TIME_MS 40
  16 #define ABP060MG_MIN_COUNTS   1638  /* = 0x0666 (10% of u14) */
  17 #define ABP060MG_MAX_COUNTS   14745 /* = 0x3999 (90% of u14) */
  18 #define ABP060MG_NUM_COUNTS   (ABP060MG_MAX_COUNTS - ABP060MG_MIN_COUNTS)
  19 
  20 enum abp_variant {
  21         /* gage [kPa] */
  22         ABP006KG, ABP010KG, ABP016KG, ABP025KG, ABP040KG, ABP060KG, ABP100KG,
  23         ABP160KG, ABP250KG, ABP400KG, ABP600KG, ABP001GG,
  24         /* differential [kPa] */
  25         ABP006KD, ABP010KD, ABP016KD, ABP025KD, ABP040KD, ABP060KD, ABP100KD,
  26         ABP160KD, ABP250KD, ABP400KD,
  27         /* gage [psi] */
  28         ABP001PG, ABP005PG, ABP015PG, ABP030PG, ABP060PG, ABP100PG, ABP150PG,
  29         /* differential [psi] */
  30         ABP001PD, ABP005PD, ABP015PD, ABP030PD, ABP060PD,
  31 };
  32 
  33 struct abp_config {
  34         int min;
  35         int max;
  36 };
  37 
  38 static struct abp_config abp_config[] = {
  39         /* mbar & kPa variants */
  40         [ABP006KG] = { .min =       0, .max =     6000 },
  41         [ABP010KG] = { .min =       0, .max =    10000 },
  42         [ABP016KG] = { .min =       0, .max =    16000 },
  43         [ABP025KG] = { .min =       0, .max =    25000 },
  44         [ABP040KG] = { .min =       0, .max =    40000 },
  45         [ABP060KG] = { .min =       0, .max =    60000 },
  46         [ABP100KG] = { .min =       0, .max =   100000 },
  47         [ABP160KG] = { .min =       0, .max =   160000 },
  48         [ABP250KG] = { .min =       0, .max =   250000 },
  49         [ABP400KG] = { .min =       0, .max =   400000 },
  50         [ABP600KG] = { .min =       0, .max =   600000 },
  51         [ABP001GG] = { .min =       0, .max =  1000000 },
  52         [ABP006KD] = { .min =   -6000, .max =     6000 },
  53         [ABP010KD] = { .min =  -10000, .max =    10000 },
  54         [ABP016KD] = { .min =  -16000, .max =    16000 },
  55         [ABP025KD] = { .min =  -25000, .max =    25000 },
  56         [ABP040KD] = { .min =  -40000, .max =    40000 },
  57         [ABP060KD] = { .min =  -60000, .max =    60000 },
  58         [ABP100KD] = { .min = -100000, .max =   100000 },
  59         [ABP160KD] = { .min = -160000, .max =   160000 },
  60         [ABP250KD] = { .min = -250000, .max =   250000 },
  61         [ABP400KD] = { .min = -400000, .max =   400000 },
  62         /* psi variants (1 psi ~ 6895 Pa) */
  63         [ABP001PG] = { .min =       0, .max =     6985 },
  64         [ABP005PG] = { .min =       0, .max =    34474 },
  65         [ABP015PG] = { .min =       0, .max =   103421 },
  66         [ABP030PG] = { .min =       0, .max =   206843 },
  67         [ABP060PG] = { .min =       0, .max =   413686 },
  68         [ABP100PG] = { .min =       0, .max =   689476 },
  69         [ABP150PG] = { .min =       0, .max =  1034214 },
  70         [ABP001PD] = { .min =   -6895, .max =     6895 },
  71         [ABP005PD] = { .min =  -34474, .max =    34474 },
  72         [ABP015PD] = { .min = -103421, .max =   103421 },
  73         [ABP030PD] = { .min = -206843, .max =   206843 },
  74         [ABP060PD] = { .min = -413686, .max =   413686 },
  75 };
  76 
  77 struct abp_state {
  78         struct i2c_client *client;
  79         struct mutex lock;
  80 
  81         /*
  82          * bus-dependent MEASURE_REQUEST length.
  83          * If no SMBUS_QUICK support, need to send dummy byte
  84          */
  85         int mreq_len;
  86 
  87         /* model-dependent values (calculated on probe) */
  88         int scale;
  89         int offset;
  90 };
  91 
  92 static const struct iio_chan_spec abp060mg_channels[] = {
  93         {
  94                 .type = IIO_PRESSURE,
  95                 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
  96                         BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_SCALE),
  97         },
  98 };
  99 
 100 static int abp060mg_get_measurement(struct abp_state *state, int *val)
 101 {
 102         struct i2c_client *client = state->client;
 103         __be16 buf[2];
 104         u16 pressure;
 105         int ret;
 106 
 107         buf[0] = 0;
 108         ret = i2c_master_send(client, (u8 *)&buf, state->mreq_len);
 109         if (ret < 0)
 110                 return ret;
 111 
 112         msleep_interruptible(ABP060MG_RESP_TIME_MS);
 113 
 114         ret = i2c_master_recv(client, (u8 *)&buf, sizeof(buf));
 115         if (ret < 0)
 116                 return ret;
 117 
 118         pressure = be16_to_cpu(buf[0]);
 119         if (pressure & ABP060MG_ERROR_MASK)
 120                 return -EIO;
 121 
 122         if (pressure < ABP060MG_MIN_COUNTS || pressure > ABP060MG_MAX_COUNTS)
 123                 return -EIO;
 124 
 125         *val = pressure;
 126 
 127         return IIO_VAL_INT;
 128 }
 129 
 130 static int abp060mg_read_raw(struct iio_dev *indio_dev,
 131                         struct iio_chan_spec const *chan, int *val,
 132                         int *val2, long mask)
 133 {
 134         struct abp_state *state = iio_priv(indio_dev);
 135         int ret;
 136 
 137         mutex_lock(&state->lock);
 138 
 139         switch (mask) {
 140         case IIO_CHAN_INFO_RAW:
 141                 ret = abp060mg_get_measurement(state, val);
 142                 break;
 143         case IIO_CHAN_INFO_OFFSET:
 144                 *val = state->offset;
 145                 ret = IIO_VAL_INT;
 146                 break;
 147         case IIO_CHAN_INFO_SCALE:
 148                 *val = state->scale;
 149                 *val2 = ABP060MG_NUM_COUNTS * 1000; /* to kPa */
 150                 ret = IIO_VAL_FRACTIONAL;
 151                 break;
 152         default:
 153                 ret = -EINVAL;
 154                 break;
 155         }
 156 
 157         mutex_unlock(&state->lock);
 158         return ret;
 159 }
 160 
 161 static const struct iio_info abp060mg_info = {
 162         .read_raw = abp060mg_read_raw,
 163 };
 164 
 165 static void abp060mg_init_device(struct iio_dev *indio_dev, unsigned long id)
 166 {
 167         struct abp_state *state = iio_priv(indio_dev);
 168         struct abp_config *cfg = &abp_config[id];
 169 
 170         state->scale = cfg->max - cfg->min;
 171         state->offset = -ABP060MG_MIN_COUNTS;
 172 
 173         if (cfg->min < 0) /* differential */
 174                 state->offset -= ABP060MG_NUM_COUNTS >> 1;
 175 }
 176 
 177 static int abp060mg_probe(struct i2c_client *client,
 178                         const struct i2c_device_id *id)
 179 {
 180         struct iio_dev *indio_dev;
 181         struct abp_state *state;
 182         unsigned long cfg_id = id->driver_data;
 183 
 184         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*state));
 185         if (!indio_dev)
 186                 return -ENOMEM;
 187 
 188         state = iio_priv(indio_dev);
 189         i2c_set_clientdata(client, state);
 190         state->client = client;
 191 
 192         if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_QUICK))
 193                 state->mreq_len = 1;
 194 
 195         abp060mg_init_device(indio_dev, cfg_id);
 196 
 197         indio_dev->dev.parent = &client->dev;
 198         indio_dev->name = dev_name(&client->dev);
 199         indio_dev->modes = INDIO_DIRECT_MODE;
 200         indio_dev->info = &abp060mg_info;
 201 
 202         indio_dev->channels = abp060mg_channels;
 203         indio_dev->num_channels = ARRAY_SIZE(abp060mg_channels);
 204 
 205         mutex_init(&state->lock);
 206 
 207         return devm_iio_device_register(&client->dev, indio_dev);
 208 }
 209 
 210 static const struct i2c_device_id abp060mg_id_table[] = {
 211         /* mbar & kPa variants (abp060m [60 mbar] == abp006k [6 kPa]) */
 212         /*    gage: */
 213         { "abp060mg", ABP006KG }, { "abp006kg", ABP006KG },
 214         { "abp100mg", ABP010KG }, { "abp010kg", ABP010KG },
 215         { "abp160mg", ABP016KG }, { "abp016kg", ABP016KG },
 216         { "abp250mg", ABP025KG }, { "abp025kg", ABP025KG },
 217         { "abp400mg", ABP040KG }, { "abp040kg", ABP040KG },
 218         { "abp600mg", ABP060KG }, { "abp060kg", ABP060KG },
 219         { "abp001bg", ABP100KG }, { "abp100kg", ABP100KG },
 220         { "abp1_6bg", ABP160KG }, { "abp160kg", ABP160KG },
 221         { "abp2_5bg", ABP250KG }, { "abp250kg", ABP250KG },
 222         { "abp004bg", ABP400KG }, { "abp400kg", ABP400KG },
 223         { "abp006bg", ABP600KG }, { "abp600kg", ABP600KG },
 224         { "abp010bg", ABP001GG }, { "abp001gg", ABP001GG },
 225         /*    differential: */
 226         { "abp060md", ABP006KD }, { "abp006kd", ABP006KD },
 227         { "abp100md", ABP010KD }, { "abp010kd", ABP010KD },
 228         { "abp160md", ABP016KD }, { "abp016kd", ABP016KD },
 229         { "abp250md", ABP025KD }, { "abp025kd", ABP025KD },
 230         { "abp400md", ABP040KD }, { "abp040kd", ABP040KD },
 231         { "abp600md", ABP060KD }, { "abp060kd", ABP060KD },
 232         { "abp001bd", ABP100KD }, { "abp100kd", ABP100KD },
 233         { "abp1_6bd", ABP160KD }, { "abp160kd", ABP160KD },
 234         { "abp2_5bd", ABP250KD }, { "abp250kd", ABP250KD },
 235         { "abp004bd", ABP400KD }, { "abp400kd", ABP400KD },
 236         /* psi variants */
 237         /*    gage: */
 238         { "abp001pg", ABP001PG },
 239         { "abp005pg", ABP005PG },
 240         { "abp015pg", ABP015PG },
 241         { "abp030pg", ABP030PG },
 242         { "abp060pg", ABP060PG },
 243         { "abp100pg", ABP100PG },
 244         { "abp150pg", ABP150PG },
 245         /*    differential: */
 246         { "abp001pd", ABP001PD },
 247         { "abp005pd", ABP005PD },
 248         { "abp015pd", ABP015PD },
 249         { "abp030pd", ABP030PD },
 250         { "abp060pd", ABP060PD },
 251         { /* empty */ },
 252 };
 253 MODULE_DEVICE_TABLE(i2c, abp060mg_id_table);
 254 
 255 static struct i2c_driver abp060mg_driver = {
 256         .driver = {
 257                 .name = "abp060mg",
 258         },
 259         .probe = abp060mg_probe,
 260         .id_table = abp060mg_id_table,
 261 };
 262 module_i2c_driver(abp060mg_driver);
 263 
 264 MODULE_AUTHOR("Marcin Malagowski <mrc@bourne.st>");
 265 MODULE_DESCRIPTION("Honeywell ABP pressure sensor driver");
 266 MODULE_LICENSE("GPL");

/* [<][>][^][v][top][bottom][index][help] */