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