18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2016 - Marcin Malagowski <mrc@bourne.st> 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci#include <linux/delay.h> 68c2ecf20Sopenharmony_ci#include <linux/device.h> 78c2ecf20Sopenharmony_ci#include <linux/err.h> 88c2ecf20Sopenharmony_ci#include <linux/i2c.h> 98c2ecf20Sopenharmony_ci#include <linux/io.h> 108c2ecf20Sopenharmony_ci#include <linux/kernel.h> 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/iio/iio.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#define ABP060MG_ERROR_MASK 0xC000 158c2ecf20Sopenharmony_ci#define ABP060MG_RESP_TIME_MS 40 168c2ecf20Sopenharmony_ci#define ABP060MG_MIN_COUNTS 1638 /* = 0x0666 (10% of u14) */ 178c2ecf20Sopenharmony_ci#define ABP060MG_MAX_COUNTS 14745 /* = 0x3999 (90% of u14) */ 188c2ecf20Sopenharmony_ci#define ABP060MG_NUM_COUNTS (ABP060MG_MAX_COUNTS - ABP060MG_MIN_COUNTS) 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_cienum abp_variant { 218c2ecf20Sopenharmony_ci /* gage [kPa] */ 228c2ecf20Sopenharmony_ci ABP006KG, ABP010KG, ABP016KG, ABP025KG, ABP040KG, ABP060KG, ABP100KG, 238c2ecf20Sopenharmony_ci ABP160KG, ABP250KG, ABP400KG, ABP600KG, ABP001GG, 248c2ecf20Sopenharmony_ci /* differential [kPa] */ 258c2ecf20Sopenharmony_ci ABP006KD, ABP010KD, ABP016KD, ABP025KD, ABP040KD, ABP060KD, ABP100KD, 268c2ecf20Sopenharmony_ci ABP160KD, ABP250KD, ABP400KD, 278c2ecf20Sopenharmony_ci /* gage [psi] */ 288c2ecf20Sopenharmony_ci ABP001PG, ABP005PG, ABP015PG, ABP030PG, ABP060PG, ABP100PG, ABP150PG, 298c2ecf20Sopenharmony_ci /* differential [psi] */ 308c2ecf20Sopenharmony_ci ABP001PD, ABP005PD, ABP015PD, ABP030PD, ABP060PD, 318c2ecf20Sopenharmony_ci}; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_cistruct abp_config { 348c2ecf20Sopenharmony_ci int min; 358c2ecf20Sopenharmony_ci int max; 368c2ecf20Sopenharmony_ci}; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistatic struct abp_config abp_config[] = { 398c2ecf20Sopenharmony_ci /* mbar & kPa variants */ 408c2ecf20Sopenharmony_ci [ABP006KG] = { .min = 0, .max = 6000 }, 418c2ecf20Sopenharmony_ci [ABP010KG] = { .min = 0, .max = 10000 }, 428c2ecf20Sopenharmony_ci [ABP016KG] = { .min = 0, .max = 16000 }, 438c2ecf20Sopenharmony_ci [ABP025KG] = { .min = 0, .max = 25000 }, 448c2ecf20Sopenharmony_ci [ABP040KG] = { .min = 0, .max = 40000 }, 458c2ecf20Sopenharmony_ci [ABP060KG] = { .min = 0, .max = 60000 }, 468c2ecf20Sopenharmony_ci [ABP100KG] = { .min = 0, .max = 100000 }, 478c2ecf20Sopenharmony_ci [ABP160KG] = { .min = 0, .max = 160000 }, 488c2ecf20Sopenharmony_ci [ABP250KG] = { .min = 0, .max = 250000 }, 498c2ecf20Sopenharmony_ci [ABP400KG] = { .min = 0, .max = 400000 }, 508c2ecf20Sopenharmony_ci [ABP600KG] = { .min = 0, .max = 600000 }, 518c2ecf20Sopenharmony_ci [ABP001GG] = { .min = 0, .max = 1000000 }, 528c2ecf20Sopenharmony_ci [ABP006KD] = { .min = -6000, .max = 6000 }, 538c2ecf20Sopenharmony_ci [ABP010KD] = { .min = -10000, .max = 10000 }, 548c2ecf20Sopenharmony_ci [ABP016KD] = { .min = -16000, .max = 16000 }, 558c2ecf20Sopenharmony_ci [ABP025KD] = { .min = -25000, .max = 25000 }, 568c2ecf20Sopenharmony_ci [ABP040KD] = { .min = -40000, .max = 40000 }, 578c2ecf20Sopenharmony_ci [ABP060KD] = { .min = -60000, .max = 60000 }, 588c2ecf20Sopenharmony_ci [ABP100KD] = { .min = -100000, .max = 100000 }, 598c2ecf20Sopenharmony_ci [ABP160KD] = { .min = -160000, .max = 160000 }, 608c2ecf20Sopenharmony_ci [ABP250KD] = { .min = -250000, .max = 250000 }, 618c2ecf20Sopenharmony_ci [ABP400KD] = { .min = -400000, .max = 400000 }, 628c2ecf20Sopenharmony_ci /* psi variants (1 psi ~ 6895 Pa) */ 638c2ecf20Sopenharmony_ci [ABP001PG] = { .min = 0, .max = 6985 }, 648c2ecf20Sopenharmony_ci [ABP005PG] = { .min = 0, .max = 34474 }, 658c2ecf20Sopenharmony_ci [ABP015PG] = { .min = 0, .max = 103421 }, 668c2ecf20Sopenharmony_ci [ABP030PG] = { .min = 0, .max = 206843 }, 678c2ecf20Sopenharmony_ci [ABP060PG] = { .min = 0, .max = 413686 }, 688c2ecf20Sopenharmony_ci [ABP100PG] = { .min = 0, .max = 689476 }, 698c2ecf20Sopenharmony_ci [ABP150PG] = { .min = 0, .max = 1034214 }, 708c2ecf20Sopenharmony_ci [ABP001PD] = { .min = -6895, .max = 6895 }, 718c2ecf20Sopenharmony_ci [ABP005PD] = { .min = -34474, .max = 34474 }, 728c2ecf20Sopenharmony_ci [ABP015PD] = { .min = -103421, .max = 103421 }, 738c2ecf20Sopenharmony_ci [ABP030PD] = { .min = -206843, .max = 206843 }, 748c2ecf20Sopenharmony_ci [ABP060PD] = { .min = -413686, .max = 413686 }, 758c2ecf20Sopenharmony_ci}; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_cistruct abp_state { 788c2ecf20Sopenharmony_ci struct i2c_client *client; 798c2ecf20Sopenharmony_ci struct mutex lock; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci /* 828c2ecf20Sopenharmony_ci * bus-dependent MEASURE_REQUEST length. 838c2ecf20Sopenharmony_ci * If no SMBUS_QUICK support, need to send dummy byte 848c2ecf20Sopenharmony_ci */ 858c2ecf20Sopenharmony_ci int mreq_len; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci /* model-dependent values (calculated on probe) */ 888c2ecf20Sopenharmony_ci int scale; 898c2ecf20Sopenharmony_ci int offset; 908c2ecf20Sopenharmony_ci}; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cistatic const struct iio_chan_spec abp060mg_channels[] = { 938c2ecf20Sopenharmony_ci { 948c2ecf20Sopenharmony_ci .type = IIO_PRESSURE, 958c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 968c2ecf20Sopenharmony_ci BIT(IIO_CHAN_INFO_OFFSET) | BIT(IIO_CHAN_INFO_SCALE), 978c2ecf20Sopenharmony_ci }, 988c2ecf20Sopenharmony_ci}; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cistatic int abp060mg_get_measurement(struct abp_state *state, int *val) 1018c2ecf20Sopenharmony_ci{ 1028c2ecf20Sopenharmony_ci struct i2c_client *client = state->client; 1038c2ecf20Sopenharmony_ci __be16 buf[2]; 1048c2ecf20Sopenharmony_ci u16 pressure; 1058c2ecf20Sopenharmony_ci int ret; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci buf[0] = 0; 1088c2ecf20Sopenharmony_ci ret = i2c_master_send(client, (u8 *)&buf, state->mreq_len); 1098c2ecf20Sopenharmony_ci if (ret < 0) 1108c2ecf20Sopenharmony_ci return ret; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci msleep_interruptible(ABP060MG_RESP_TIME_MS); 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci ret = i2c_master_recv(client, (u8 *)&buf, sizeof(buf)); 1158c2ecf20Sopenharmony_ci if (ret < 0) 1168c2ecf20Sopenharmony_ci return ret; 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci pressure = be16_to_cpu(buf[0]); 1198c2ecf20Sopenharmony_ci if (pressure & ABP060MG_ERROR_MASK) 1208c2ecf20Sopenharmony_ci return -EIO; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci if (pressure < ABP060MG_MIN_COUNTS || pressure > ABP060MG_MAX_COUNTS) 1238c2ecf20Sopenharmony_ci return -EIO; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci *val = pressure; 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci return IIO_VAL_INT; 1288c2ecf20Sopenharmony_ci} 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_cistatic int abp060mg_read_raw(struct iio_dev *indio_dev, 1318c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, int *val, 1328c2ecf20Sopenharmony_ci int *val2, long mask) 1338c2ecf20Sopenharmony_ci{ 1348c2ecf20Sopenharmony_ci struct abp_state *state = iio_priv(indio_dev); 1358c2ecf20Sopenharmony_ci int ret; 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci mutex_lock(&state->lock); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci switch (mask) { 1408c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_RAW: 1418c2ecf20Sopenharmony_ci ret = abp060mg_get_measurement(state, val); 1428c2ecf20Sopenharmony_ci break; 1438c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_OFFSET: 1448c2ecf20Sopenharmony_ci *val = state->offset; 1458c2ecf20Sopenharmony_ci ret = IIO_VAL_INT; 1468c2ecf20Sopenharmony_ci break; 1478c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SCALE: 1488c2ecf20Sopenharmony_ci *val = state->scale; 1498c2ecf20Sopenharmony_ci *val2 = ABP060MG_NUM_COUNTS * 1000; /* to kPa */ 1508c2ecf20Sopenharmony_ci ret = IIO_VAL_FRACTIONAL; 1518c2ecf20Sopenharmony_ci break; 1528c2ecf20Sopenharmony_ci default: 1538c2ecf20Sopenharmony_ci ret = -EINVAL; 1548c2ecf20Sopenharmony_ci break; 1558c2ecf20Sopenharmony_ci } 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci mutex_unlock(&state->lock); 1588c2ecf20Sopenharmony_ci return ret; 1598c2ecf20Sopenharmony_ci} 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_cistatic const struct iio_info abp060mg_info = { 1628c2ecf20Sopenharmony_ci .read_raw = abp060mg_read_raw, 1638c2ecf20Sopenharmony_ci}; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_cistatic void abp060mg_init_device(struct iio_dev *indio_dev, unsigned long id) 1668c2ecf20Sopenharmony_ci{ 1678c2ecf20Sopenharmony_ci struct abp_state *state = iio_priv(indio_dev); 1688c2ecf20Sopenharmony_ci struct abp_config *cfg = &abp_config[id]; 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci state->scale = cfg->max - cfg->min; 1718c2ecf20Sopenharmony_ci state->offset = -ABP060MG_MIN_COUNTS; 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci if (cfg->min < 0) /* differential */ 1748c2ecf20Sopenharmony_ci state->offset -= ABP060MG_NUM_COUNTS >> 1; 1758c2ecf20Sopenharmony_ci} 1768c2ecf20Sopenharmony_ci 1778c2ecf20Sopenharmony_cistatic int abp060mg_probe(struct i2c_client *client, 1788c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 1798c2ecf20Sopenharmony_ci{ 1808c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 1818c2ecf20Sopenharmony_ci struct abp_state *state; 1828c2ecf20Sopenharmony_ci unsigned long cfg_id = id->driver_data; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*state)); 1858c2ecf20Sopenharmony_ci if (!indio_dev) 1868c2ecf20Sopenharmony_ci return -ENOMEM; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci state = iio_priv(indio_dev); 1898c2ecf20Sopenharmony_ci i2c_set_clientdata(client, state); 1908c2ecf20Sopenharmony_ci state->client = client; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_QUICK)) 1938c2ecf20Sopenharmony_ci state->mreq_len = 1; 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci abp060mg_init_device(indio_dev, cfg_id); 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci indio_dev->name = dev_name(&client->dev); 1988c2ecf20Sopenharmony_ci indio_dev->modes = INDIO_DIRECT_MODE; 1998c2ecf20Sopenharmony_ci indio_dev->info = &abp060mg_info; 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci indio_dev->channels = abp060mg_channels; 2028c2ecf20Sopenharmony_ci indio_dev->num_channels = ARRAY_SIZE(abp060mg_channels); 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci mutex_init(&state->lock); 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci return devm_iio_device_register(&client->dev, indio_dev); 2078c2ecf20Sopenharmony_ci} 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_cistatic const struct i2c_device_id abp060mg_id_table[] = { 2108c2ecf20Sopenharmony_ci /* mbar & kPa variants (abp060m [60 mbar] == abp006k [6 kPa]) */ 2118c2ecf20Sopenharmony_ci /* gage: */ 2128c2ecf20Sopenharmony_ci { "abp060mg", ABP006KG }, { "abp006kg", ABP006KG }, 2138c2ecf20Sopenharmony_ci { "abp100mg", ABP010KG }, { "abp010kg", ABP010KG }, 2148c2ecf20Sopenharmony_ci { "abp160mg", ABP016KG }, { "abp016kg", ABP016KG }, 2158c2ecf20Sopenharmony_ci { "abp250mg", ABP025KG }, { "abp025kg", ABP025KG }, 2168c2ecf20Sopenharmony_ci { "abp400mg", ABP040KG }, { "abp040kg", ABP040KG }, 2178c2ecf20Sopenharmony_ci { "abp600mg", ABP060KG }, { "abp060kg", ABP060KG }, 2188c2ecf20Sopenharmony_ci { "abp001bg", ABP100KG }, { "abp100kg", ABP100KG }, 2198c2ecf20Sopenharmony_ci { "abp1_6bg", ABP160KG }, { "abp160kg", ABP160KG }, 2208c2ecf20Sopenharmony_ci { "abp2_5bg", ABP250KG }, { "abp250kg", ABP250KG }, 2218c2ecf20Sopenharmony_ci { "abp004bg", ABP400KG }, { "abp400kg", ABP400KG }, 2228c2ecf20Sopenharmony_ci { "abp006bg", ABP600KG }, { "abp600kg", ABP600KG }, 2238c2ecf20Sopenharmony_ci { "abp010bg", ABP001GG }, { "abp001gg", ABP001GG }, 2248c2ecf20Sopenharmony_ci /* differential: */ 2258c2ecf20Sopenharmony_ci { "abp060md", ABP006KD }, { "abp006kd", ABP006KD }, 2268c2ecf20Sopenharmony_ci { "abp100md", ABP010KD }, { "abp010kd", ABP010KD }, 2278c2ecf20Sopenharmony_ci { "abp160md", ABP016KD }, { "abp016kd", ABP016KD }, 2288c2ecf20Sopenharmony_ci { "abp250md", ABP025KD }, { "abp025kd", ABP025KD }, 2298c2ecf20Sopenharmony_ci { "abp400md", ABP040KD }, { "abp040kd", ABP040KD }, 2308c2ecf20Sopenharmony_ci { "abp600md", ABP060KD }, { "abp060kd", ABP060KD }, 2318c2ecf20Sopenharmony_ci { "abp001bd", ABP100KD }, { "abp100kd", ABP100KD }, 2328c2ecf20Sopenharmony_ci { "abp1_6bd", ABP160KD }, { "abp160kd", ABP160KD }, 2338c2ecf20Sopenharmony_ci { "abp2_5bd", ABP250KD }, { "abp250kd", ABP250KD }, 2348c2ecf20Sopenharmony_ci { "abp004bd", ABP400KD }, { "abp400kd", ABP400KD }, 2358c2ecf20Sopenharmony_ci /* psi variants */ 2368c2ecf20Sopenharmony_ci /* gage: */ 2378c2ecf20Sopenharmony_ci { "abp001pg", ABP001PG }, 2388c2ecf20Sopenharmony_ci { "abp005pg", ABP005PG }, 2398c2ecf20Sopenharmony_ci { "abp015pg", ABP015PG }, 2408c2ecf20Sopenharmony_ci { "abp030pg", ABP030PG }, 2418c2ecf20Sopenharmony_ci { "abp060pg", ABP060PG }, 2428c2ecf20Sopenharmony_ci { "abp100pg", ABP100PG }, 2438c2ecf20Sopenharmony_ci { "abp150pg", ABP150PG }, 2448c2ecf20Sopenharmony_ci /* differential: */ 2458c2ecf20Sopenharmony_ci { "abp001pd", ABP001PD }, 2468c2ecf20Sopenharmony_ci { "abp005pd", ABP005PD }, 2478c2ecf20Sopenharmony_ci { "abp015pd", ABP015PD }, 2488c2ecf20Sopenharmony_ci { "abp030pd", ABP030PD }, 2498c2ecf20Sopenharmony_ci { "abp060pd", ABP060PD }, 2508c2ecf20Sopenharmony_ci { /* empty */ }, 2518c2ecf20Sopenharmony_ci}; 2528c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(i2c, abp060mg_id_table); 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_cistatic struct i2c_driver abp060mg_driver = { 2558c2ecf20Sopenharmony_ci .driver = { 2568c2ecf20Sopenharmony_ci .name = "abp060mg", 2578c2ecf20Sopenharmony_ci }, 2588c2ecf20Sopenharmony_ci .probe = abp060mg_probe, 2598c2ecf20Sopenharmony_ci .id_table = abp060mg_id_table, 2608c2ecf20Sopenharmony_ci}; 2618c2ecf20Sopenharmony_cimodule_i2c_driver(abp060mg_driver); 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ciMODULE_AUTHOR("Marcin Malagowski <mrc@bourne.st>"); 2648c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Honeywell ABP pressure sensor driver"); 2658c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 266