Matt Ranostay | d6ad805 | 2018-02-17 21:36:46 -0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 2 | /* |
| 3 | * atlas-ph-sensor.c - Support for Atlas Scientific OEM pH-SM sensor |
| 4 | * |
Matt Ranostay | d6ad805 | 2018-02-17 21:36:46 -0800 | [diff] [blame] | 5 | * Copyright (C) 2015-2018 Matt Ranostay |
| 6 | * Author: Matt Ranostay <matt.ranostay@konsulko.com> |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/init.h> |
| 11 | #include <linux/interrupt.h> |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/mutex.h> |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/irq.h> |
| 16 | #include <linux/irq_work.h> |
| 17 | #include <linux/gpio.h> |
| 18 | #include <linux/i2c.h> |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 19 | #include <linux/of_device.h> |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 20 | #include <linux/regmap.h> |
| 21 | #include <linux/iio/iio.h> |
| 22 | #include <linux/iio/buffer.h> |
| 23 | #include <linux/iio/trigger.h> |
| 24 | #include <linux/iio/trigger_consumer.h> |
| 25 | #include <linux/iio/triggered_buffer.h> |
| 26 | #include <linux/pm_runtime.h> |
| 27 | |
| 28 | #define ATLAS_REGMAP_NAME "atlas_ph_regmap" |
| 29 | #define ATLAS_DRV_NAME "atlas_ph" |
| 30 | |
| 31 | #define ATLAS_REG_DEV_TYPE 0x00 |
| 32 | #define ATLAS_REG_DEV_VERSION 0x01 |
| 33 | |
| 34 | #define ATLAS_REG_INT_CONTROL 0x04 |
| 35 | #define ATLAS_REG_INT_CONTROL_EN BIT(3) |
| 36 | |
| 37 | #define ATLAS_REG_PWR_CONTROL 0x06 |
| 38 | |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 39 | #define ATLAS_REG_PH_CALIB_STATUS 0x0d |
| 40 | #define ATLAS_REG_PH_CALIB_STATUS_MASK 0x07 |
| 41 | #define ATLAS_REG_PH_CALIB_STATUS_LOW BIT(0) |
| 42 | #define ATLAS_REG_PH_CALIB_STATUS_MID BIT(1) |
| 43 | #define ATLAS_REG_PH_CALIB_STATUS_HIGH BIT(2) |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 44 | |
Matt Ranostay | e8dd92b | 2016-05-24 21:29:20 -0700 | [diff] [blame] | 45 | #define ATLAS_REG_EC_CALIB_STATUS 0x0f |
| 46 | #define ATLAS_REG_EC_CALIB_STATUS_MASK 0x0f |
| 47 | #define ATLAS_REG_EC_CALIB_STATUS_DRY BIT(0) |
| 48 | #define ATLAS_REG_EC_CALIB_STATUS_SINGLE BIT(1) |
| 49 | #define ATLAS_REG_EC_CALIB_STATUS_LOW BIT(2) |
| 50 | #define ATLAS_REG_EC_CALIB_STATUS_HIGH BIT(3) |
| 51 | |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 52 | #define ATLAS_REG_PH_TEMP_DATA 0x0e |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 53 | #define ATLAS_REG_PH_DATA 0x16 |
| 54 | |
Matt Ranostay | e8dd92b | 2016-05-24 21:29:20 -0700 | [diff] [blame] | 55 | #define ATLAS_REG_EC_PROBE 0x08 |
| 56 | #define ATLAS_REG_EC_TEMP_DATA 0x10 |
| 57 | #define ATLAS_REG_EC_DATA 0x18 |
| 58 | #define ATLAS_REG_TDS_DATA 0x1c |
| 59 | #define ATLAS_REG_PSS_DATA 0x20 |
| 60 | |
Matt Ranostay | ce08cc9 | 2016-08-19 13:36:11 -0700 | [diff] [blame] | 61 | #define ATLAS_REG_ORP_CALIB_STATUS 0x0d |
| 62 | #define ATLAS_REG_ORP_DATA 0x0e |
| 63 | |
Matt Ranostay | 0211226 | 2018-04-09 23:18:22 -0700 | [diff] [blame^] | 64 | #define ATLAS_PH_INT_TIME_IN_MS 450 |
| 65 | #define ATLAS_EC_INT_TIME_IN_MS 650 |
| 66 | #define ATLAS_ORP_INT_TIME_IN_MS 450 |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 67 | |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 68 | enum { |
| 69 | ATLAS_PH_SM, |
Matt Ranostay | e8dd92b | 2016-05-24 21:29:20 -0700 | [diff] [blame] | 70 | ATLAS_EC_SM, |
Matt Ranostay | ce08cc9 | 2016-08-19 13:36:11 -0700 | [diff] [blame] | 71 | ATLAS_ORP_SM, |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 74 | struct atlas_data { |
| 75 | struct i2c_client *client; |
| 76 | struct iio_trigger *trig; |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 77 | struct atlas_device *chip; |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 78 | struct regmap *regmap; |
| 79 | struct irq_work work; |
| 80 | |
Matt Ranostay | e8dd92b | 2016-05-24 21:29:20 -0700 | [diff] [blame] | 81 | __be32 buffer[6]; /* 96-bit data + 32-bit pad + 64-bit timestamp */ |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 82 | }; |
| 83 | |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 84 | static const struct regmap_config atlas_regmap_config = { |
| 85 | .name = ATLAS_REGMAP_NAME, |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 86 | .reg_bits = 8, |
| 87 | .val_bits = 8, |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 90 | static const struct iio_chan_spec atlas_ph_channels[] = { |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 91 | { |
| 92 | .type = IIO_PH, |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 93 | .address = ATLAS_REG_PH_DATA, |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 94 | .info_mask_separate = |
| 95 | BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), |
| 96 | .scan_index = 0, |
| 97 | .scan_type = { |
| 98 | .sign = 'u', |
| 99 | .realbits = 32, |
| 100 | .storagebits = 32, |
| 101 | .endianness = IIO_BE, |
| 102 | }, |
| 103 | }, |
| 104 | IIO_CHAN_SOFT_TIMESTAMP(1), |
| 105 | { |
| 106 | .type = IIO_TEMP, |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 107 | .address = ATLAS_REG_PH_TEMP_DATA, |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 108 | .info_mask_separate = |
| 109 | BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), |
| 110 | .output = 1, |
| 111 | .scan_index = -1 |
| 112 | }, |
| 113 | }; |
| 114 | |
Matt Ranostay | e8dd92b | 2016-05-24 21:29:20 -0700 | [diff] [blame] | 115 | #define ATLAS_EC_CHANNEL(_idx, _addr) \ |
| 116 | {\ |
| 117 | .type = IIO_CONCENTRATION, \ |
| 118 | .indexed = 1, \ |
| 119 | .channel = _idx, \ |
| 120 | .address = _addr, \ |
| 121 | .info_mask_separate = \ |
| 122 | BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), \ |
| 123 | .scan_index = _idx + 1, \ |
| 124 | .scan_type = { \ |
| 125 | .sign = 'u', \ |
| 126 | .realbits = 32, \ |
| 127 | .storagebits = 32, \ |
| 128 | .endianness = IIO_BE, \ |
| 129 | }, \ |
| 130 | } |
| 131 | |
| 132 | static const struct iio_chan_spec atlas_ec_channels[] = { |
| 133 | { |
| 134 | .type = IIO_ELECTRICALCONDUCTIVITY, |
| 135 | .address = ATLAS_REG_EC_DATA, |
| 136 | .info_mask_separate = |
| 137 | BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), |
| 138 | .scan_index = 0, |
| 139 | .scan_type = { |
| 140 | .sign = 'u', |
| 141 | .realbits = 32, |
| 142 | .storagebits = 32, |
| 143 | .endianness = IIO_BE, |
| 144 | }, |
| 145 | }, |
| 146 | ATLAS_EC_CHANNEL(0, ATLAS_REG_TDS_DATA), |
| 147 | ATLAS_EC_CHANNEL(1, ATLAS_REG_PSS_DATA), |
| 148 | IIO_CHAN_SOFT_TIMESTAMP(3), |
| 149 | { |
| 150 | .type = IIO_TEMP, |
| 151 | .address = ATLAS_REG_EC_TEMP_DATA, |
| 152 | .info_mask_separate = |
| 153 | BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), |
| 154 | .output = 1, |
| 155 | .scan_index = -1 |
| 156 | }, |
| 157 | }; |
| 158 | |
Matt Ranostay | ce08cc9 | 2016-08-19 13:36:11 -0700 | [diff] [blame] | 159 | static const struct iio_chan_spec atlas_orp_channels[] = { |
| 160 | { |
| 161 | .type = IIO_VOLTAGE, |
| 162 | .address = ATLAS_REG_ORP_DATA, |
| 163 | .info_mask_separate = |
| 164 | BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), |
| 165 | .scan_index = 0, |
| 166 | .scan_type = { |
| 167 | .sign = 's', |
| 168 | .realbits = 32, |
| 169 | .storagebits = 32, |
| 170 | .endianness = IIO_BE, |
| 171 | }, |
| 172 | }, |
| 173 | IIO_CHAN_SOFT_TIMESTAMP(1), |
| 174 | }; |
| 175 | |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 176 | static int atlas_check_ph_calibration(struct atlas_data *data) |
| 177 | { |
| 178 | struct device *dev = &data->client->dev; |
| 179 | int ret; |
| 180 | unsigned int val; |
| 181 | |
| 182 | ret = regmap_read(data->regmap, ATLAS_REG_PH_CALIB_STATUS, &val); |
| 183 | if (ret) |
| 184 | return ret; |
| 185 | |
| 186 | if (!(val & ATLAS_REG_PH_CALIB_STATUS_MASK)) { |
| 187 | dev_warn(dev, "device has not been calibrated\n"); |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | if (!(val & ATLAS_REG_PH_CALIB_STATUS_LOW)) |
| 192 | dev_warn(dev, "device missing low point calibration\n"); |
| 193 | |
| 194 | if (!(val & ATLAS_REG_PH_CALIB_STATUS_MID)) |
| 195 | dev_warn(dev, "device missing mid point calibration\n"); |
| 196 | |
| 197 | if (!(val & ATLAS_REG_PH_CALIB_STATUS_HIGH)) |
| 198 | dev_warn(dev, "device missing high point calibration\n"); |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Matt Ranostay | e8dd92b | 2016-05-24 21:29:20 -0700 | [diff] [blame] | 203 | static int atlas_check_ec_calibration(struct atlas_data *data) |
| 204 | { |
| 205 | struct device *dev = &data->client->dev; |
| 206 | int ret; |
| 207 | unsigned int val; |
Sandhya Bankar | d1fe85e | 2016-09-25 00:46:21 +0530 | [diff] [blame] | 208 | __be16 rval; |
Matt Ranostay | e8dd92b | 2016-05-24 21:29:20 -0700 | [diff] [blame] | 209 | |
Sandhya Bankar | d1fe85e | 2016-09-25 00:46:21 +0530 | [diff] [blame] | 210 | ret = regmap_bulk_read(data->regmap, ATLAS_REG_EC_PROBE, &rval, 2); |
Matt Ranostay | e8dd92b | 2016-05-24 21:29:20 -0700 | [diff] [blame] | 211 | if (ret) |
| 212 | return ret; |
| 213 | |
Sandhya Bankar | d1fe85e | 2016-09-25 00:46:21 +0530 | [diff] [blame] | 214 | val = be16_to_cpu(rval); |
| 215 | dev_info(dev, "probe set to K = %d.%.2d", val / 100, val % 100); |
Matt Ranostay | e8dd92b | 2016-05-24 21:29:20 -0700 | [diff] [blame] | 216 | |
| 217 | ret = regmap_read(data->regmap, ATLAS_REG_EC_CALIB_STATUS, &val); |
| 218 | if (ret) |
| 219 | return ret; |
| 220 | |
| 221 | if (!(val & ATLAS_REG_EC_CALIB_STATUS_MASK)) { |
| 222 | dev_warn(dev, "device has not been calibrated\n"); |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | if (!(val & ATLAS_REG_EC_CALIB_STATUS_DRY)) |
| 227 | dev_warn(dev, "device missing dry point calibration\n"); |
| 228 | |
| 229 | if (val & ATLAS_REG_EC_CALIB_STATUS_SINGLE) { |
| 230 | dev_warn(dev, "device using single point calibration\n"); |
| 231 | } else { |
| 232 | if (!(val & ATLAS_REG_EC_CALIB_STATUS_LOW)) |
| 233 | dev_warn(dev, "device missing low point calibration\n"); |
| 234 | |
| 235 | if (!(val & ATLAS_REG_EC_CALIB_STATUS_HIGH)) |
| 236 | dev_warn(dev, "device missing high point calibration\n"); |
| 237 | } |
| 238 | |
| 239 | return 0; |
| 240 | } |
| 241 | |
Matt Ranostay | ce08cc9 | 2016-08-19 13:36:11 -0700 | [diff] [blame] | 242 | static int atlas_check_orp_calibration(struct atlas_data *data) |
| 243 | { |
| 244 | struct device *dev = &data->client->dev; |
| 245 | int ret; |
| 246 | unsigned int val; |
| 247 | |
| 248 | ret = regmap_read(data->regmap, ATLAS_REG_ORP_CALIB_STATUS, &val); |
| 249 | if (ret) |
| 250 | return ret; |
| 251 | |
| 252 | if (!val) |
| 253 | dev_warn(dev, "device has not been calibrated\n"); |
| 254 | |
| 255 | return 0; |
| 256 | }; |
| 257 | |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 258 | struct atlas_device { |
| 259 | const struct iio_chan_spec *channels; |
| 260 | int num_channels; |
| 261 | int data_reg; |
| 262 | |
| 263 | int (*calibration)(struct atlas_data *data); |
| 264 | int delay; |
| 265 | }; |
| 266 | |
| 267 | static struct atlas_device atlas_devices[] = { |
| 268 | [ATLAS_PH_SM] = { |
| 269 | .channels = atlas_ph_channels, |
| 270 | .num_channels = 3, |
| 271 | .data_reg = ATLAS_REG_PH_DATA, |
| 272 | .calibration = &atlas_check_ph_calibration, |
Matt Ranostay | 0211226 | 2018-04-09 23:18:22 -0700 | [diff] [blame^] | 273 | .delay = ATLAS_PH_INT_TIME_IN_MS, |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 274 | }, |
Matt Ranostay | e8dd92b | 2016-05-24 21:29:20 -0700 | [diff] [blame] | 275 | [ATLAS_EC_SM] = { |
| 276 | .channels = atlas_ec_channels, |
| 277 | .num_channels = 5, |
| 278 | .data_reg = ATLAS_REG_EC_DATA, |
| 279 | .calibration = &atlas_check_ec_calibration, |
Matt Ranostay | 0211226 | 2018-04-09 23:18:22 -0700 | [diff] [blame^] | 280 | .delay = ATLAS_EC_INT_TIME_IN_MS, |
Matt Ranostay | e8dd92b | 2016-05-24 21:29:20 -0700 | [diff] [blame] | 281 | }, |
Matt Ranostay | ce08cc9 | 2016-08-19 13:36:11 -0700 | [diff] [blame] | 282 | [ATLAS_ORP_SM] = { |
| 283 | .channels = atlas_orp_channels, |
| 284 | .num_channels = 2, |
| 285 | .data_reg = ATLAS_REG_ORP_DATA, |
| 286 | .calibration = &atlas_check_orp_calibration, |
Matt Ranostay | 0211226 | 2018-04-09 23:18:22 -0700 | [diff] [blame^] | 287 | .delay = ATLAS_ORP_INT_TIME_IN_MS, |
Matt Ranostay | ce08cc9 | 2016-08-19 13:36:11 -0700 | [diff] [blame] | 288 | }, |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 289 | }; |
| 290 | |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 291 | static int atlas_set_powermode(struct atlas_data *data, int on) |
| 292 | { |
| 293 | return regmap_write(data->regmap, ATLAS_REG_PWR_CONTROL, on); |
| 294 | } |
| 295 | |
| 296 | static int atlas_set_interrupt(struct atlas_data *data, bool state) |
| 297 | { |
| 298 | return regmap_update_bits(data->regmap, ATLAS_REG_INT_CONTROL, |
| 299 | ATLAS_REG_INT_CONTROL_EN, |
| 300 | state ? ATLAS_REG_INT_CONTROL_EN : 0); |
| 301 | } |
| 302 | |
| 303 | static int atlas_buffer_postenable(struct iio_dev *indio_dev) |
| 304 | { |
| 305 | struct atlas_data *data = iio_priv(indio_dev); |
| 306 | int ret; |
| 307 | |
| 308 | ret = iio_triggered_buffer_postenable(indio_dev); |
| 309 | if (ret) |
| 310 | return ret; |
| 311 | |
| 312 | ret = pm_runtime_get_sync(&data->client->dev); |
| 313 | if (ret < 0) { |
| 314 | pm_runtime_put_noidle(&data->client->dev); |
| 315 | return ret; |
| 316 | } |
| 317 | |
| 318 | return atlas_set_interrupt(data, true); |
| 319 | } |
| 320 | |
| 321 | static int atlas_buffer_predisable(struct iio_dev *indio_dev) |
| 322 | { |
| 323 | struct atlas_data *data = iio_priv(indio_dev); |
| 324 | int ret; |
| 325 | |
| 326 | ret = iio_triggered_buffer_predisable(indio_dev); |
| 327 | if (ret) |
| 328 | return ret; |
| 329 | |
| 330 | ret = atlas_set_interrupt(data, false); |
| 331 | if (ret) |
| 332 | return ret; |
| 333 | |
| 334 | pm_runtime_mark_last_busy(&data->client->dev); |
| 335 | return pm_runtime_put_autosuspend(&data->client->dev); |
| 336 | } |
| 337 | |
| 338 | static const struct iio_trigger_ops atlas_interrupt_trigger_ops = { |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 339 | }; |
| 340 | |
| 341 | static const struct iio_buffer_setup_ops atlas_buffer_setup_ops = { |
| 342 | .postenable = atlas_buffer_postenable, |
| 343 | .predisable = atlas_buffer_predisable, |
| 344 | }; |
| 345 | |
| 346 | static void atlas_work_handler(struct irq_work *work) |
| 347 | { |
| 348 | struct atlas_data *data = container_of(work, struct atlas_data, work); |
| 349 | |
| 350 | iio_trigger_poll(data->trig); |
| 351 | } |
| 352 | |
| 353 | static irqreturn_t atlas_trigger_handler(int irq, void *private) |
| 354 | { |
| 355 | struct iio_poll_func *pf = private; |
| 356 | struct iio_dev *indio_dev = pf->indio_dev; |
| 357 | struct atlas_data *data = iio_priv(indio_dev); |
| 358 | int ret; |
| 359 | |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 360 | ret = regmap_bulk_read(data->regmap, data->chip->data_reg, |
| 361 | (u8 *) &data->buffer, |
| 362 | sizeof(__be32) * (data->chip->num_channels - 2)); |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 363 | |
Matt Ranostay | 98e55e9 | 2016-02-14 18:59:07 -0800 | [diff] [blame] | 364 | if (!ret) |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 365 | iio_push_to_buffers_with_timestamp(indio_dev, data->buffer, |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 366 | iio_get_time_ns(indio_dev)); |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 367 | |
| 368 | iio_trigger_notify_done(indio_dev->trig); |
| 369 | |
| 370 | return IRQ_HANDLED; |
| 371 | } |
| 372 | |
| 373 | static irqreturn_t atlas_interrupt_handler(int irq, void *private) |
| 374 | { |
| 375 | struct iio_dev *indio_dev = private; |
| 376 | struct atlas_data *data = iio_priv(indio_dev); |
| 377 | |
| 378 | irq_work_queue(&data->work); |
| 379 | |
| 380 | return IRQ_HANDLED; |
| 381 | } |
| 382 | |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 383 | static int atlas_read_measurement(struct atlas_data *data, int reg, __be32 *val) |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 384 | { |
| 385 | struct device *dev = &data->client->dev; |
| 386 | int suspended = pm_runtime_suspended(dev); |
| 387 | int ret; |
| 388 | |
| 389 | ret = pm_runtime_get_sync(dev); |
| 390 | if (ret < 0) { |
| 391 | pm_runtime_put_noidle(dev); |
| 392 | return ret; |
| 393 | } |
| 394 | |
| 395 | if (suspended) |
Matt Ranostay | 0211226 | 2018-04-09 23:18:22 -0700 | [diff] [blame^] | 396 | msleep(data->chip->delay); |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 397 | |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 398 | ret = regmap_bulk_read(data->regmap, reg, (u8 *) val, sizeof(*val)); |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 399 | |
| 400 | pm_runtime_mark_last_busy(dev); |
| 401 | pm_runtime_put_autosuspend(dev); |
| 402 | |
| 403 | return ret; |
| 404 | } |
| 405 | |
| 406 | static int atlas_read_raw(struct iio_dev *indio_dev, |
| 407 | struct iio_chan_spec const *chan, |
| 408 | int *val, int *val2, long mask) |
| 409 | { |
| 410 | struct atlas_data *data = iio_priv(indio_dev); |
| 411 | |
| 412 | switch (mask) { |
| 413 | case IIO_CHAN_INFO_RAW: { |
| 414 | int ret; |
| 415 | __be32 reg; |
| 416 | |
| 417 | switch (chan->type) { |
| 418 | case IIO_TEMP: |
| 419 | ret = regmap_bulk_read(data->regmap, chan->address, |
| 420 | (u8 *) ®, sizeof(reg)); |
| 421 | break; |
| 422 | case IIO_PH: |
Matt Ranostay | e8dd92b | 2016-05-24 21:29:20 -0700 | [diff] [blame] | 423 | case IIO_CONCENTRATION: |
| 424 | case IIO_ELECTRICALCONDUCTIVITY: |
Matt Ranostay | ce08cc9 | 2016-08-19 13:36:11 -0700 | [diff] [blame] | 425 | case IIO_VOLTAGE: |
Matt Ranostay | b015b3e | 2016-08-19 13:36:09 -0700 | [diff] [blame] | 426 | ret = iio_device_claim_direct_mode(indio_dev); |
| 427 | if (ret) |
| 428 | return ret; |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 429 | |
Matt Ranostay | b015b3e | 2016-08-19 13:36:09 -0700 | [diff] [blame] | 430 | ret = atlas_read_measurement(data, chan->address, ®); |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 431 | |
Matt Ranostay | b015b3e | 2016-08-19 13:36:09 -0700 | [diff] [blame] | 432 | iio_device_release_direct_mode(indio_dev); |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 433 | break; |
| 434 | default: |
| 435 | ret = -EINVAL; |
| 436 | } |
| 437 | |
| 438 | if (!ret) { |
| 439 | *val = be32_to_cpu(reg); |
| 440 | ret = IIO_VAL_INT; |
| 441 | } |
| 442 | return ret; |
| 443 | } |
| 444 | case IIO_CHAN_INFO_SCALE: |
| 445 | switch (chan->type) { |
| 446 | case IIO_TEMP: |
| 447 | *val = 1; /* 0.01 */ |
| 448 | *val2 = 100; |
| 449 | break; |
| 450 | case IIO_PH: |
| 451 | *val = 1; /* 0.001 */ |
| 452 | *val2 = 1000; |
| 453 | break; |
Matt Ranostay | e8dd92b | 2016-05-24 21:29:20 -0700 | [diff] [blame] | 454 | case IIO_ELECTRICALCONDUCTIVITY: |
| 455 | *val = 1; /* 0.00001 */ |
Colin Ian King | ca64d4b | 2016-07-25 23:06:56 +0100 | [diff] [blame] | 456 | *val2 = 100000; |
Matt Ranostay | e8dd92b | 2016-05-24 21:29:20 -0700 | [diff] [blame] | 457 | break; |
| 458 | case IIO_CONCENTRATION: |
| 459 | *val = 0; /* 0.000000001 */ |
| 460 | *val2 = 1000; |
| 461 | return IIO_VAL_INT_PLUS_NANO; |
Matt Ranostay | ce08cc9 | 2016-08-19 13:36:11 -0700 | [diff] [blame] | 462 | case IIO_VOLTAGE: |
| 463 | *val = 1; /* 0.1 */ |
| 464 | *val2 = 10; |
| 465 | break; |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 466 | default: |
| 467 | return -EINVAL; |
| 468 | } |
| 469 | return IIO_VAL_FRACTIONAL; |
| 470 | } |
| 471 | |
| 472 | return -EINVAL; |
| 473 | } |
| 474 | |
| 475 | static int atlas_write_raw(struct iio_dev *indio_dev, |
| 476 | struct iio_chan_spec const *chan, |
| 477 | int val, int val2, long mask) |
| 478 | { |
| 479 | struct atlas_data *data = iio_priv(indio_dev); |
| 480 | __be32 reg = cpu_to_be32(val); |
| 481 | |
| 482 | if (val2 != 0 || val < 0 || val > 20000) |
| 483 | return -EINVAL; |
| 484 | |
| 485 | if (mask != IIO_CHAN_INFO_RAW || chan->type != IIO_TEMP) |
| 486 | return -EINVAL; |
| 487 | |
| 488 | return regmap_bulk_write(data->regmap, chan->address, |
| 489 | ®, sizeof(reg)); |
| 490 | } |
| 491 | |
| 492 | static const struct iio_info atlas_info = { |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 493 | .read_raw = atlas_read_raw, |
| 494 | .write_raw = atlas_write_raw, |
| 495 | }; |
| 496 | |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 497 | static const struct i2c_device_id atlas_id[] = { |
| 498 | { "atlas-ph-sm", ATLAS_PH_SM}, |
Matt Ranostay | e8dd92b | 2016-05-24 21:29:20 -0700 | [diff] [blame] | 499 | { "atlas-ec-sm", ATLAS_EC_SM}, |
Matt Ranostay | ce08cc9 | 2016-08-19 13:36:11 -0700 | [diff] [blame] | 500 | { "atlas-orp-sm", ATLAS_ORP_SM}, |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 501 | {} |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 502 | }; |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 503 | MODULE_DEVICE_TABLE(i2c, atlas_id); |
| 504 | |
| 505 | static const struct of_device_id atlas_dt_ids[] = { |
| 506 | { .compatible = "atlas,ph-sm", .data = (void *)ATLAS_PH_SM, }, |
Matt Ranostay | e8dd92b | 2016-05-24 21:29:20 -0700 | [diff] [blame] | 507 | { .compatible = "atlas,ec-sm", .data = (void *)ATLAS_EC_SM, }, |
Matt Ranostay | ce08cc9 | 2016-08-19 13:36:11 -0700 | [diff] [blame] | 508 | { .compatible = "atlas,orp-sm", .data = (void *)ATLAS_ORP_SM, }, |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 509 | { } |
| 510 | }; |
| 511 | MODULE_DEVICE_TABLE(of, atlas_dt_ids); |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 512 | |
| 513 | static int atlas_probe(struct i2c_client *client, |
| 514 | const struct i2c_device_id *id) |
| 515 | { |
| 516 | struct atlas_data *data; |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 517 | struct atlas_device *chip; |
| 518 | const struct of_device_id *of_id; |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 519 | struct iio_trigger *trig; |
| 520 | struct iio_dev *indio_dev; |
| 521 | int ret; |
| 522 | |
| 523 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
| 524 | if (!indio_dev) |
| 525 | return -ENOMEM; |
| 526 | |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 527 | of_id = of_match_device(atlas_dt_ids, &client->dev); |
| 528 | if (!of_id) |
| 529 | chip = &atlas_devices[id->driver_data]; |
| 530 | else |
| 531 | chip = &atlas_devices[(unsigned long)of_id->data]; |
| 532 | |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 533 | indio_dev->info = &atlas_info; |
| 534 | indio_dev->name = ATLAS_DRV_NAME; |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 535 | indio_dev->channels = chip->channels; |
| 536 | indio_dev->num_channels = chip->num_channels; |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 537 | indio_dev->modes = INDIO_BUFFER_SOFTWARE | INDIO_DIRECT_MODE; |
| 538 | indio_dev->dev.parent = &client->dev; |
| 539 | |
| 540 | trig = devm_iio_trigger_alloc(&client->dev, "%s-dev%d", |
| 541 | indio_dev->name, indio_dev->id); |
| 542 | |
| 543 | if (!trig) |
| 544 | return -ENOMEM; |
| 545 | |
| 546 | data = iio_priv(indio_dev); |
| 547 | data->client = client; |
| 548 | data->trig = trig; |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 549 | data->chip = chip; |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 550 | trig->dev.parent = indio_dev->dev.parent; |
| 551 | trig->ops = &atlas_interrupt_trigger_ops; |
| 552 | iio_trigger_set_drvdata(trig, indio_dev); |
| 553 | |
| 554 | i2c_set_clientdata(client, indio_dev); |
| 555 | |
| 556 | data->regmap = devm_regmap_init_i2c(client, &atlas_regmap_config); |
| 557 | if (IS_ERR(data->regmap)) { |
| 558 | dev_err(&client->dev, "regmap initialization failed\n"); |
| 559 | return PTR_ERR(data->regmap); |
| 560 | } |
| 561 | |
| 562 | ret = pm_runtime_set_active(&client->dev); |
| 563 | if (ret) |
| 564 | return ret; |
| 565 | |
| 566 | if (client->irq <= 0) { |
| 567 | dev_err(&client->dev, "no valid irq defined\n"); |
| 568 | return -EINVAL; |
| 569 | } |
| 570 | |
Matt Ranostay | 7103b99 | 2016-05-24 21:29:18 -0700 | [diff] [blame] | 571 | ret = chip->calibration(data); |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 572 | if (ret) |
| 573 | return ret; |
| 574 | |
| 575 | ret = iio_trigger_register(trig); |
| 576 | if (ret) { |
| 577 | dev_err(&client->dev, "failed to register trigger\n"); |
| 578 | return ret; |
| 579 | } |
| 580 | |
| 581 | ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, |
| 582 | &atlas_trigger_handler, &atlas_buffer_setup_ops); |
| 583 | if (ret) { |
| 584 | dev_err(&client->dev, "cannot setup iio trigger\n"); |
| 585 | goto unregister_trigger; |
| 586 | } |
| 587 | |
| 588 | init_irq_work(&data->work, atlas_work_handler); |
| 589 | |
| 590 | /* interrupt pin toggles on new conversion */ |
| 591 | ret = devm_request_threaded_irq(&client->dev, client->irq, |
| 592 | NULL, atlas_interrupt_handler, |
| 593 | IRQF_TRIGGER_RISING | |
| 594 | IRQF_TRIGGER_FALLING | IRQF_ONESHOT, |
| 595 | "atlas_irq", |
| 596 | indio_dev); |
| 597 | if (ret) { |
| 598 | dev_err(&client->dev, "request irq (%d) failed\n", client->irq); |
| 599 | goto unregister_buffer; |
| 600 | } |
| 601 | |
| 602 | ret = atlas_set_powermode(data, 1); |
| 603 | if (ret) { |
| 604 | dev_err(&client->dev, "cannot power device on"); |
| 605 | goto unregister_buffer; |
| 606 | } |
| 607 | |
| 608 | pm_runtime_enable(&client->dev); |
| 609 | pm_runtime_set_autosuspend_delay(&client->dev, 2500); |
| 610 | pm_runtime_use_autosuspend(&client->dev); |
| 611 | |
| 612 | ret = iio_device_register(indio_dev); |
| 613 | if (ret) { |
| 614 | dev_err(&client->dev, "unable to register device\n"); |
| 615 | goto unregister_pm; |
| 616 | } |
| 617 | |
| 618 | return 0; |
| 619 | |
| 620 | unregister_pm: |
| 621 | pm_runtime_disable(&client->dev); |
| 622 | atlas_set_powermode(data, 0); |
| 623 | |
| 624 | unregister_buffer: |
| 625 | iio_triggered_buffer_cleanup(indio_dev); |
| 626 | |
| 627 | unregister_trigger: |
| 628 | iio_trigger_unregister(data->trig); |
| 629 | |
| 630 | return ret; |
| 631 | } |
| 632 | |
| 633 | static int atlas_remove(struct i2c_client *client) |
| 634 | { |
| 635 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 636 | struct atlas_data *data = iio_priv(indio_dev); |
| 637 | |
| 638 | iio_device_unregister(indio_dev); |
| 639 | iio_triggered_buffer_cleanup(indio_dev); |
| 640 | iio_trigger_unregister(data->trig); |
| 641 | |
| 642 | pm_runtime_disable(&client->dev); |
| 643 | pm_runtime_set_suspended(&client->dev); |
| 644 | pm_runtime_put_noidle(&client->dev); |
| 645 | |
| 646 | return atlas_set_powermode(data, 0); |
| 647 | } |
| 648 | |
| 649 | #ifdef CONFIG_PM |
| 650 | static int atlas_runtime_suspend(struct device *dev) |
| 651 | { |
| 652 | struct atlas_data *data = |
| 653 | iio_priv(i2c_get_clientdata(to_i2c_client(dev))); |
| 654 | |
| 655 | return atlas_set_powermode(data, 0); |
| 656 | } |
| 657 | |
| 658 | static int atlas_runtime_resume(struct device *dev) |
| 659 | { |
| 660 | struct atlas_data *data = |
| 661 | iio_priv(i2c_get_clientdata(to_i2c_client(dev))); |
| 662 | |
| 663 | return atlas_set_powermode(data, 1); |
| 664 | } |
| 665 | #endif |
| 666 | |
| 667 | static const struct dev_pm_ops atlas_pm_ops = { |
| 668 | SET_RUNTIME_PM_OPS(atlas_runtime_suspend, |
| 669 | atlas_runtime_resume, NULL) |
| 670 | }; |
| 671 | |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 672 | static struct i2c_driver atlas_driver = { |
| 673 | .driver = { |
| 674 | .name = ATLAS_DRV_NAME, |
| 675 | .of_match_table = of_match_ptr(atlas_dt_ids), |
| 676 | .pm = &atlas_pm_ops, |
| 677 | }, |
| 678 | .probe = atlas_probe, |
| 679 | .remove = atlas_remove, |
| 680 | .id_table = atlas_id, |
| 681 | }; |
| 682 | module_i2c_driver(atlas_driver); |
| 683 | |
Matt Ranostay | d6ad805 | 2018-02-17 21:36:46 -0800 | [diff] [blame] | 684 | MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>"); |
Matt Ranostay | 27dec00 | 2016-01-26 18:34:31 -0800 | [diff] [blame] | 685 | MODULE_DESCRIPTION("Atlas Scientific pH-SM sensor"); |
| 686 | MODULE_LICENSE("GPL"); |