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