rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * AFE4404 Heart Rate Monitors and Low-Cost Pulse Oximeters |
| 3 | * |
| 4 | * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/ |
| 5 | * Andrew F. Davis <afd@ti.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/i2c.h> |
| 21 | #include <linux/kernel.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/regmap.h> |
| 24 | #include <linux/sysfs.h> |
| 25 | #include <linux/regulator/consumer.h> |
| 26 | |
| 27 | #include <linux/iio/iio.h> |
| 28 | #include <linux/iio/sysfs.h> |
| 29 | #include <linux/iio/buffer.h> |
| 30 | #include <linux/iio/trigger.h> |
| 31 | #include <linux/iio/triggered_buffer.h> |
| 32 | #include <linux/iio/trigger_consumer.h> |
| 33 | |
| 34 | #include "afe440x.h" |
| 35 | |
| 36 | #define AFE4404_DRIVER_NAME "afe4404" |
| 37 | |
| 38 | /* AFE4404 registers */ |
| 39 | #define AFE4404_TIA_GAIN_SEP 0x20 |
| 40 | #define AFE4404_TIA_GAIN 0x21 |
| 41 | #define AFE4404_PROG_TG_STC 0x34 |
| 42 | #define AFE4404_PROG_TG_ENDC 0x35 |
| 43 | #define AFE4404_LED3LEDSTC 0x36 |
| 44 | #define AFE4404_LED3LEDENDC 0x37 |
| 45 | #define AFE4404_CLKDIV_PRF 0x39 |
| 46 | #define AFE4404_OFFDAC 0x3a |
| 47 | #define AFE4404_DEC 0x3d |
| 48 | #define AFE4404_AVG_LED2_ALED2VAL 0x3f |
| 49 | #define AFE4404_AVG_LED1_ALED1VAL 0x40 |
| 50 | |
| 51 | /* AFE4404 CONTROL2 register fields */ |
| 52 | #define AFE440X_CONTROL2_OSC_ENABLE BIT(9) |
| 53 | |
| 54 | enum afe4404_fields { |
| 55 | /* Gains */ |
| 56 | F_TIA_GAIN_SEP, F_TIA_CF_SEP, |
| 57 | F_TIA_GAIN, TIA_CF, |
| 58 | |
| 59 | /* LED Current */ |
| 60 | F_ILED1, F_ILED2, F_ILED3, |
| 61 | |
| 62 | /* Offset DAC */ |
| 63 | F_OFFDAC_AMB2, F_OFFDAC_LED1, F_OFFDAC_AMB1, F_OFFDAC_LED2, |
| 64 | |
| 65 | /* sentinel */ |
| 66 | F_MAX_FIELDS |
| 67 | }; |
| 68 | |
| 69 | static const struct reg_field afe4404_reg_fields[] = { |
| 70 | /* Gains */ |
| 71 | [F_TIA_GAIN_SEP] = REG_FIELD(AFE4404_TIA_GAIN_SEP, 0, 2), |
| 72 | [F_TIA_CF_SEP] = REG_FIELD(AFE4404_TIA_GAIN_SEP, 3, 5), |
| 73 | [F_TIA_GAIN] = REG_FIELD(AFE4404_TIA_GAIN, 0, 2), |
| 74 | [TIA_CF] = REG_FIELD(AFE4404_TIA_GAIN, 3, 5), |
| 75 | /* LED Current */ |
| 76 | [F_ILED1] = REG_FIELD(AFE440X_LEDCNTRL, 0, 5), |
| 77 | [F_ILED2] = REG_FIELD(AFE440X_LEDCNTRL, 6, 11), |
| 78 | [F_ILED3] = REG_FIELD(AFE440X_LEDCNTRL, 12, 17), |
| 79 | /* Offset DAC */ |
| 80 | [F_OFFDAC_AMB2] = REG_FIELD(AFE4404_OFFDAC, 0, 4), |
| 81 | [F_OFFDAC_LED1] = REG_FIELD(AFE4404_OFFDAC, 5, 9), |
| 82 | [F_OFFDAC_AMB1] = REG_FIELD(AFE4404_OFFDAC, 10, 14), |
| 83 | [F_OFFDAC_LED2] = REG_FIELD(AFE4404_OFFDAC, 15, 19), |
| 84 | }; |
| 85 | |
| 86 | /** |
| 87 | * struct afe4404_data - AFE4404 device instance data |
| 88 | * @dev: Device structure |
| 89 | * @regmap: Register map of the device |
| 90 | * @fields: Register fields of the device |
| 91 | * @regulator: Pointer to the regulator for the IC |
| 92 | * @trig: IIO trigger for this device |
| 93 | * @irq: ADC_RDY line interrupt number |
| 94 | * @buffer: Used to construct a scan to push to the iio buffer. |
| 95 | */ |
| 96 | struct afe4404_data { |
| 97 | struct device *dev; |
| 98 | struct regmap *regmap; |
| 99 | struct regmap_field *fields[F_MAX_FIELDS]; |
| 100 | struct regulator *regulator; |
| 101 | struct iio_trigger *trig; |
| 102 | int irq; |
| 103 | s32 buffer[10] __aligned(8); |
| 104 | }; |
| 105 | |
| 106 | enum afe4404_chan_id { |
| 107 | LED2 = 1, |
| 108 | ALED2, |
| 109 | LED1, |
| 110 | ALED1, |
| 111 | LED2_ALED2, |
| 112 | LED1_ALED1, |
| 113 | }; |
| 114 | |
| 115 | static const unsigned int afe4404_channel_values[] = { |
| 116 | [LED2] = AFE440X_LED2VAL, |
| 117 | [ALED2] = AFE440X_ALED2VAL, |
| 118 | [LED1] = AFE440X_LED1VAL, |
| 119 | [ALED1] = AFE440X_ALED1VAL, |
| 120 | [LED2_ALED2] = AFE440X_LED2_ALED2VAL, |
| 121 | [LED1_ALED1] = AFE440X_LED1_ALED1VAL, |
| 122 | }; |
| 123 | |
| 124 | static const unsigned int afe4404_channel_leds[] = { |
| 125 | [LED2] = F_ILED2, |
| 126 | [ALED2] = F_ILED3, |
| 127 | [LED1] = F_ILED1, |
| 128 | }; |
| 129 | |
| 130 | static const unsigned int afe4404_channel_offdacs[] = { |
| 131 | [LED2] = F_OFFDAC_LED2, |
| 132 | [ALED2] = F_OFFDAC_AMB2, |
| 133 | [LED1] = F_OFFDAC_LED1, |
| 134 | [ALED1] = F_OFFDAC_AMB1, |
| 135 | }; |
| 136 | |
| 137 | static const struct iio_chan_spec afe4404_channels[] = { |
| 138 | /* ADC values */ |
| 139 | AFE440X_INTENSITY_CHAN(LED2, BIT(IIO_CHAN_INFO_OFFSET)), |
| 140 | AFE440X_INTENSITY_CHAN(ALED2, BIT(IIO_CHAN_INFO_OFFSET)), |
| 141 | AFE440X_INTENSITY_CHAN(LED1, BIT(IIO_CHAN_INFO_OFFSET)), |
| 142 | AFE440X_INTENSITY_CHAN(ALED1, BIT(IIO_CHAN_INFO_OFFSET)), |
| 143 | AFE440X_INTENSITY_CHAN(LED2_ALED2, 0), |
| 144 | AFE440X_INTENSITY_CHAN(LED1_ALED1, 0), |
| 145 | /* LED current */ |
| 146 | AFE440X_CURRENT_CHAN(LED2), |
| 147 | AFE440X_CURRENT_CHAN(ALED2), |
| 148 | AFE440X_CURRENT_CHAN(LED1), |
| 149 | }; |
| 150 | |
| 151 | static const struct afe440x_val_table afe4404_res_table[] = { |
| 152 | { .integer = 500000, .fract = 0 }, |
| 153 | { .integer = 250000, .fract = 0 }, |
| 154 | { .integer = 100000, .fract = 0 }, |
| 155 | { .integer = 50000, .fract = 0 }, |
| 156 | { .integer = 25000, .fract = 0 }, |
| 157 | { .integer = 10000, .fract = 0 }, |
| 158 | { .integer = 1000000, .fract = 0 }, |
| 159 | { .integer = 2000000, .fract = 0 }, |
| 160 | }; |
| 161 | AFE440X_TABLE_ATTR(in_intensity_resistance_available, afe4404_res_table); |
| 162 | |
| 163 | static const struct afe440x_val_table afe4404_cap_table[] = { |
| 164 | { .integer = 0, .fract = 5000 }, |
| 165 | { .integer = 0, .fract = 2500 }, |
| 166 | { .integer = 0, .fract = 10000 }, |
| 167 | { .integer = 0, .fract = 7500 }, |
| 168 | { .integer = 0, .fract = 20000 }, |
| 169 | { .integer = 0, .fract = 17500 }, |
| 170 | { .integer = 0, .fract = 25000 }, |
| 171 | { .integer = 0, .fract = 22500 }, |
| 172 | }; |
| 173 | AFE440X_TABLE_ATTR(in_intensity_capacitance_available, afe4404_cap_table); |
| 174 | |
| 175 | static ssize_t afe440x_show_register(struct device *dev, |
| 176 | struct device_attribute *attr, |
| 177 | char *buf) |
| 178 | { |
| 179 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 180 | struct afe4404_data *afe = iio_priv(indio_dev); |
| 181 | struct afe440x_attr *afe440x_attr = to_afe440x_attr(attr); |
| 182 | unsigned int reg_val; |
| 183 | int vals[2]; |
| 184 | int ret; |
| 185 | |
| 186 | ret = regmap_field_read(afe->fields[afe440x_attr->field], ®_val); |
| 187 | if (ret) |
| 188 | return ret; |
| 189 | |
| 190 | if (reg_val >= afe440x_attr->table_size) |
| 191 | return -EINVAL; |
| 192 | |
| 193 | vals[0] = afe440x_attr->val_table[reg_val].integer; |
| 194 | vals[1] = afe440x_attr->val_table[reg_val].fract; |
| 195 | |
| 196 | return iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, 2, vals); |
| 197 | } |
| 198 | |
| 199 | static ssize_t afe440x_store_register(struct device *dev, |
| 200 | struct device_attribute *attr, |
| 201 | const char *buf, size_t count) |
| 202 | { |
| 203 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
| 204 | struct afe4404_data *afe = iio_priv(indio_dev); |
| 205 | struct afe440x_attr *afe440x_attr = to_afe440x_attr(attr); |
| 206 | int val, integer, fract, ret; |
| 207 | |
| 208 | ret = iio_str_to_fixpoint(buf, 100000, &integer, &fract); |
| 209 | if (ret) |
| 210 | return ret; |
| 211 | |
| 212 | for (val = 0; val < afe440x_attr->table_size; val++) |
| 213 | if (afe440x_attr->val_table[val].integer == integer && |
| 214 | afe440x_attr->val_table[val].fract == fract) |
| 215 | break; |
| 216 | if (val == afe440x_attr->table_size) |
| 217 | return -EINVAL; |
| 218 | |
| 219 | ret = regmap_field_write(afe->fields[afe440x_attr->field], val); |
| 220 | if (ret) |
| 221 | return ret; |
| 222 | |
| 223 | return count; |
| 224 | } |
| 225 | |
| 226 | static AFE440X_ATTR(in_intensity1_resistance, F_TIA_GAIN_SEP, afe4404_res_table); |
| 227 | static AFE440X_ATTR(in_intensity1_capacitance, F_TIA_CF_SEP, afe4404_cap_table); |
| 228 | |
| 229 | static AFE440X_ATTR(in_intensity2_resistance, F_TIA_GAIN_SEP, afe4404_res_table); |
| 230 | static AFE440X_ATTR(in_intensity2_capacitance, F_TIA_CF_SEP, afe4404_cap_table); |
| 231 | |
| 232 | static AFE440X_ATTR(in_intensity3_resistance, F_TIA_GAIN, afe4404_res_table); |
| 233 | static AFE440X_ATTR(in_intensity3_capacitance, TIA_CF, afe4404_cap_table); |
| 234 | |
| 235 | static AFE440X_ATTR(in_intensity4_resistance, F_TIA_GAIN, afe4404_res_table); |
| 236 | static AFE440X_ATTR(in_intensity4_capacitance, TIA_CF, afe4404_cap_table); |
| 237 | |
| 238 | static struct attribute *afe440x_attributes[] = { |
| 239 | &dev_attr_in_intensity_resistance_available.attr, |
| 240 | &dev_attr_in_intensity_capacitance_available.attr, |
| 241 | &afe440x_attr_in_intensity1_resistance.dev_attr.attr, |
| 242 | &afe440x_attr_in_intensity1_capacitance.dev_attr.attr, |
| 243 | &afe440x_attr_in_intensity2_resistance.dev_attr.attr, |
| 244 | &afe440x_attr_in_intensity2_capacitance.dev_attr.attr, |
| 245 | &afe440x_attr_in_intensity3_resistance.dev_attr.attr, |
| 246 | &afe440x_attr_in_intensity3_capacitance.dev_attr.attr, |
| 247 | &afe440x_attr_in_intensity4_resistance.dev_attr.attr, |
| 248 | &afe440x_attr_in_intensity4_capacitance.dev_attr.attr, |
| 249 | NULL |
| 250 | }; |
| 251 | |
| 252 | static const struct attribute_group afe440x_attribute_group = { |
| 253 | .attrs = afe440x_attributes |
| 254 | }; |
| 255 | |
| 256 | static int afe4404_read_raw(struct iio_dev *indio_dev, |
| 257 | struct iio_chan_spec const *chan, |
| 258 | int *val, int *val2, long mask) |
| 259 | { |
| 260 | struct afe4404_data *afe = iio_priv(indio_dev); |
| 261 | unsigned int value_reg = afe4404_channel_values[chan->address]; |
| 262 | unsigned int led_field = afe4404_channel_leds[chan->address]; |
| 263 | unsigned int offdac_field = afe4404_channel_offdacs[chan->address]; |
| 264 | int ret; |
| 265 | |
| 266 | switch (chan->type) { |
| 267 | case IIO_INTENSITY: |
| 268 | switch (mask) { |
| 269 | case IIO_CHAN_INFO_RAW: |
| 270 | ret = regmap_read(afe->regmap, value_reg, val); |
| 271 | if (ret) |
| 272 | return ret; |
| 273 | return IIO_VAL_INT; |
| 274 | case IIO_CHAN_INFO_OFFSET: |
| 275 | ret = regmap_field_read(afe->fields[offdac_field], val); |
| 276 | if (ret) |
| 277 | return ret; |
| 278 | return IIO_VAL_INT; |
| 279 | } |
| 280 | break; |
| 281 | case IIO_CURRENT: |
| 282 | switch (mask) { |
| 283 | case IIO_CHAN_INFO_RAW: |
| 284 | ret = regmap_field_read(afe->fields[led_field], val); |
| 285 | if (ret) |
| 286 | return ret; |
| 287 | return IIO_VAL_INT; |
| 288 | case IIO_CHAN_INFO_SCALE: |
| 289 | *val = 0; |
| 290 | *val2 = 800000; |
| 291 | return IIO_VAL_INT_PLUS_MICRO; |
| 292 | } |
| 293 | break; |
| 294 | default: |
| 295 | break; |
| 296 | } |
| 297 | |
| 298 | return -EINVAL; |
| 299 | } |
| 300 | |
| 301 | static int afe4404_write_raw(struct iio_dev *indio_dev, |
| 302 | struct iio_chan_spec const *chan, |
| 303 | int val, int val2, long mask) |
| 304 | { |
| 305 | struct afe4404_data *afe = iio_priv(indio_dev); |
| 306 | unsigned int led_field = afe4404_channel_leds[chan->address]; |
| 307 | unsigned int offdac_field = afe4404_channel_offdacs[chan->address]; |
| 308 | |
| 309 | switch (chan->type) { |
| 310 | case IIO_INTENSITY: |
| 311 | switch (mask) { |
| 312 | case IIO_CHAN_INFO_OFFSET: |
| 313 | return regmap_field_write(afe->fields[offdac_field], val); |
| 314 | } |
| 315 | break; |
| 316 | case IIO_CURRENT: |
| 317 | switch (mask) { |
| 318 | case IIO_CHAN_INFO_RAW: |
| 319 | return regmap_field_write(afe->fields[led_field], val); |
| 320 | } |
| 321 | break; |
| 322 | default: |
| 323 | break; |
| 324 | } |
| 325 | |
| 326 | return -EINVAL; |
| 327 | } |
| 328 | |
| 329 | static const struct iio_info afe4404_iio_info = { |
| 330 | .attrs = &afe440x_attribute_group, |
| 331 | .read_raw = afe4404_read_raw, |
| 332 | .write_raw = afe4404_write_raw, |
| 333 | .driver_module = THIS_MODULE, |
| 334 | }; |
| 335 | |
| 336 | static irqreturn_t afe4404_trigger_handler(int irq, void *private) |
| 337 | { |
| 338 | struct iio_poll_func *pf = private; |
| 339 | struct iio_dev *indio_dev = pf->indio_dev; |
| 340 | struct afe4404_data *afe = iio_priv(indio_dev); |
| 341 | int ret, bit, i = 0; |
| 342 | |
| 343 | for_each_set_bit(bit, indio_dev->active_scan_mask, |
| 344 | indio_dev->masklength) { |
| 345 | ret = regmap_read(afe->regmap, afe4404_channel_values[bit], |
| 346 | &afe->buffer[i++]); |
| 347 | if (ret) |
| 348 | goto err; |
| 349 | } |
| 350 | |
| 351 | iio_push_to_buffers_with_timestamp(indio_dev, afe->buffer, |
| 352 | pf->timestamp); |
| 353 | err: |
| 354 | iio_trigger_notify_done(indio_dev->trig); |
| 355 | |
| 356 | return IRQ_HANDLED; |
| 357 | } |
| 358 | |
| 359 | static const struct iio_trigger_ops afe4404_trigger_ops = { |
| 360 | .owner = THIS_MODULE, |
| 361 | }; |
| 362 | |
| 363 | /* Default timings from data-sheet */ |
| 364 | #define AFE4404_TIMING_PAIRS \ |
| 365 | { AFE440X_PRPCOUNT, 39999 }, \ |
| 366 | { AFE440X_LED2LEDSTC, 0 }, \ |
| 367 | { AFE440X_LED2LEDENDC, 398 }, \ |
| 368 | { AFE440X_LED2STC, 80 }, \ |
| 369 | { AFE440X_LED2ENDC, 398 }, \ |
| 370 | { AFE440X_ADCRSTSTCT0, 5600 }, \ |
| 371 | { AFE440X_ADCRSTENDCT0, 5606 }, \ |
| 372 | { AFE440X_LED2CONVST, 5607 }, \ |
| 373 | { AFE440X_LED2CONVEND, 6066 }, \ |
| 374 | { AFE4404_LED3LEDSTC, 400 }, \ |
| 375 | { AFE4404_LED3LEDENDC, 798 }, \ |
| 376 | { AFE440X_ALED2STC, 480 }, \ |
| 377 | { AFE440X_ALED2ENDC, 798 }, \ |
| 378 | { AFE440X_ADCRSTSTCT1, 6068 }, \ |
| 379 | { AFE440X_ADCRSTENDCT1, 6074 }, \ |
| 380 | { AFE440X_ALED2CONVST, 6075 }, \ |
| 381 | { AFE440X_ALED2CONVEND, 6534 }, \ |
| 382 | { AFE440X_LED1LEDSTC, 800 }, \ |
| 383 | { AFE440X_LED1LEDENDC, 1198 }, \ |
| 384 | { AFE440X_LED1STC, 880 }, \ |
| 385 | { AFE440X_LED1ENDC, 1198 }, \ |
| 386 | { AFE440X_ADCRSTSTCT2, 6536 }, \ |
| 387 | { AFE440X_ADCRSTENDCT2, 6542 }, \ |
| 388 | { AFE440X_LED1CONVST, 6543 }, \ |
| 389 | { AFE440X_LED1CONVEND, 7003 }, \ |
| 390 | { AFE440X_ALED1STC, 1280 }, \ |
| 391 | { AFE440X_ALED1ENDC, 1598 }, \ |
| 392 | { AFE440X_ADCRSTSTCT3, 7005 }, \ |
| 393 | { AFE440X_ADCRSTENDCT3, 7011 }, \ |
| 394 | { AFE440X_ALED1CONVST, 7012 }, \ |
| 395 | { AFE440X_ALED1CONVEND, 7471 }, \ |
| 396 | { AFE440X_PDNCYCLESTC, 7671 }, \ |
| 397 | { AFE440X_PDNCYCLEENDC, 39199 } |
| 398 | |
| 399 | static const struct reg_sequence afe4404_reg_sequences[] = { |
| 400 | AFE4404_TIMING_PAIRS, |
| 401 | { AFE440X_CONTROL1, AFE440X_CONTROL1_TIMEREN }, |
| 402 | { AFE4404_TIA_GAIN_SEP, AFE440X_TIAGAIN_ENSEPGAIN }, |
| 403 | { AFE440X_CONTROL2, AFE440X_CONTROL2_OSC_ENABLE }, |
| 404 | }; |
| 405 | |
| 406 | static const struct regmap_range afe4404_yes_ranges[] = { |
| 407 | regmap_reg_range(AFE440X_LED2VAL, AFE440X_LED1_ALED1VAL), |
| 408 | regmap_reg_range(AFE4404_AVG_LED2_ALED2VAL, AFE4404_AVG_LED1_ALED1VAL), |
| 409 | }; |
| 410 | |
| 411 | static const struct regmap_access_table afe4404_volatile_table = { |
| 412 | .yes_ranges = afe4404_yes_ranges, |
| 413 | .n_yes_ranges = ARRAY_SIZE(afe4404_yes_ranges), |
| 414 | }; |
| 415 | |
| 416 | static const struct regmap_config afe4404_regmap_config = { |
| 417 | .reg_bits = 8, |
| 418 | .val_bits = 24, |
| 419 | |
| 420 | .max_register = AFE4404_AVG_LED1_ALED1VAL, |
| 421 | .cache_type = REGCACHE_RBTREE, |
| 422 | .volatile_table = &afe4404_volatile_table, |
| 423 | }; |
| 424 | |
| 425 | static const struct of_device_id afe4404_of_match[] = { |
| 426 | { .compatible = "ti,afe4404", }, |
| 427 | { /* sentinel */ } |
| 428 | }; |
| 429 | MODULE_DEVICE_TABLE(of, afe4404_of_match); |
| 430 | |
| 431 | static int __maybe_unused afe4404_suspend(struct device *dev) |
| 432 | { |
| 433 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 434 | struct afe4404_data *afe = iio_priv(indio_dev); |
| 435 | int ret; |
| 436 | |
| 437 | ret = regmap_update_bits(afe->regmap, AFE440X_CONTROL2, |
| 438 | AFE440X_CONTROL2_PDN_AFE, |
| 439 | AFE440X_CONTROL2_PDN_AFE); |
| 440 | if (ret) |
| 441 | return ret; |
| 442 | |
| 443 | ret = regulator_disable(afe->regulator); |
| 444 | if (ret) { |
| 445 | dev_err(dev, "Unable to disable regulator\n"); |
| 446 | return ret; |
| 447 | } |
| 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | static int __maybe_unused afe4404_resume(struct device *dev) |
| 453 | { |
| 454 | struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev)); |
| 455 | struct afe4404_data *afe = iio_priv(indio_dev); |
| 456 | int ret; |
| 457 | |
| 458 | ret = regulator_enable(afe->regulator); |
| 459 | if (ret) { |
| 460 | dev_err(dev, "Unable to enable regulator\n"); |
| 461 | return ret; |
| 462 | } |
| 463 | |
| 464 | ret = regmap_update_bits(afe->regmap, AFE440X_CONTROL2, |
| 465 | AFE440X_CONTROL2_PDN_AFE, 0); |
| 466 | if (ret) |
| 467 | return ret; |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | static SIMPLE_DEV_PM_OPS(afe4404_pm_ops, afe4404_suspend, afe4404_resume); |
| 473 | |
| 474 | static int afe4404_probe(struct i2c_client *client, |
| 475 | const struct i2c_device_id *id) |
| 476 | { |
| 477 | struct iio_dev *indio_dev; |
| 478 | struct afe4404_data *afe; |
| 479 | int i, ret; |
| 480 | |
| 481 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*afe)); |
| 482 | if (!indio_dev) |
| 483 | return -ENOMEM; |
| 484 | |
| 485 | afe = iio_priv(indio_dev); |
| 486 | i2c_set_clientdata(client, indio_dev); |
| 487 | |
| 488 | afe->dev = &client->dev; |
| 489 | afe->irq = client->irq; |
| 490 | |
| 491 | afe->regmap = devm_regmap_init_i2c(client, &afe4404_regmap_config); |
| 492 | if (IS_ERR(afe->regmap)) { |
| 493 | dev_err(afe->dev, "Unable to allocate register map\n"); |
| 494 | return PTR_ERR(afe->regmap); |
| 495 | } |
| 496 | |
| 497 | for (i = 0; i < F_MAX_FIELDS; i++) { |
| 498 | afe->fields[i] = devm_regmap_field_alloc(afe->dev, afe->regmap, |
| 499 | afe4404_reg_fields[i]); |
| 500 | if (IS_ERR(afe->fields[i])) { |
| 501 | dev_err(afe->dev, "Unable to allocate regmap fields\n"); |
| 502 | return PTR_ERR(afe->fields[i]); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | afe->regulator = devm_regulator_get(afe->dev, "tx_sup"); |
| 507 | if (IS_ERR(afe->regulator)) { |
| 508 | dev_err(afe->dev, "Unable to get regulator\n"); |
| 509 | return PTR_ERR(afe->regulator); |
| 510 | } |
| 511 | ret = regulator_enable(afe->regulator); |
| 512 | if (ret) { |
| 513 | dev_err(afe->dev, "Unable to enable regulator\n"); |
| 514 | return ret; |
| 515 | } |
| 516 | |
| 517 | ret = regmap_write(afe->regmap, AFE440X_CONTROL0, |
| 518 | AFE440X_CONTROL0_SW_RESET); |
| 519 | if (ret) { |
| 520 | dev_err(afe->dev, "Unable to reset device\n"); |
| 521 | goto disable_reg; |
| 522 | } |
| 523 | |
| 524 | ret = regmap_multi_reg_write(afe->regmap, afe4404_reg_sequences, |
| 525 | ARRAY_SIZE(afe4404_reg_sequences)); |
| 526 | if (ret) { |
| 527 | dev_err(afe->dev, "Unable to set register defaults\n"); |
| 528 | goto disable_reg; |
| 529 | } |
| 530 | |
| 531 | indio_dev->modes = INDIO_DIRECT_MODE; |
| 532 | indio_dev->dev.parent = afe->dev; |
| 533 | indio_dev->channels = afe4404_channels; |
| 534 | indio_dev->num_channels = ARRAY_SIZE(afe4404_channels); |
| 535 | indio_dev->name = AFE4404_DRIVER_NAME; |
| 536 | indio_dev->info = &afe4404_iio_info; |
| 537 | |
| 538 | if (afe->irq > 0) { |
| 539 | afe->trig = devm_iio_trigger_alloc(afe->dev, |
| 540 | "%s-dev%d", |
| 541 | indio_dev->name, |
| 542 | indio_dev->id); |
| 543 | if (!afe->trig) { |
| 544 | dev_err(afe->dev, "Unable to allocate IIO trigger\n"); |
| 545 | ret = -ENOMEM; |
| 546 | goto disable_reg; |
| 547 | } |
| 548 | |
| 549 | iio_trigger_set_drvdata(afe->trig, indio_dev); |
| 550 | |
| 551 | afe->trig->ops = &afe4404_trigger_ops; |
| 552 | afe->trig->dev.parent = afe->dev; |
| 553 | |
| 554 | ret = iio_trigger_register(afe->trig); |
| 555 | if (ret) { |
| 556 | dev_err(afe->dev, "Unable to register IIO trigger\n"); |
| 557 | goto disable_reg; |
| 558 | } |
| 559 | |
| 560 | ret = devm_request_threaded_irq(afe->dev, afe->irq, |
| 561 | iio_trigger_generic_data_rdy_poll, |
| 562 | NULL, IRQF_ONESHOT, |
| 563 | AFE4404_DRIVER_NAME, |
| 564 | afe->trig); |
| 565 | if (ret) { |
| 566 | dev_err(afe->dev, "Unable to request IRQ\n"); |
| 567 | goto disable_reg; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time, |
| 572 | afe4404_trigger_handler, NULL); |
| 573 | if (ret) { |
| 574 | dev_err(afe->dev, "Unable to setup buffer\n"); |
| 575 | goto unregister_trigger; |
| 576 | } |
| 577 | |
| 578 | ret = iio_device_register(indio_dev); |
| 579 | if (ret) { |
| 580 | dev_err(afe->dev, "Unable to register IIO device\n"); |
| 581 | goto unregister_triggered_buffer; |
| 582 | } |
| 583 | |
| 584 | return 0; |
| 585 | |
| 586 | unregister_triggered_buffer: |
| 587 | iio_triggered_buffer_cleanup(indio_dev); |
| 588 | unregister_trigger: |
| 589 | if (afe->irq > 0) |
| 590 | iio_trigger_unregister(afe->trig); |
| 591 | disable_reg: |
| 592 | regulator_disable(afe->regulator); |
| 593 | |
| 594 | return ret; |
| 595 | } |
| 596 | |
| 597 | static int afe4404_remove(struct i2c_client *client) |
| 598 | { |
| 599 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
| 600 | struct afe4404_data *afe = iio_priv(indio_dev); |
| 601 | int ret; |
| 602 | |
| 603 | iio_device_unregister(indio_dev); |
| 604 | |
| 605 | iio_triggered_buffer_cleanup(indio_dev); |
| 606 | |
| 607 | if (afe->irq > 0) |
| 608 | iio_trigger_unregister(afe->trig); |
| 609 | |
| 610 | ret = regulator_disable(afe->regulator); |
| 611 | if (ret) { |
| 612 | dev_err(afe->dev, "Unable to disable regulator\n"); |
| 613 | return ret; |
| 614 | } |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | static const struct i2c_device_id afe4404_ids[] = { |
| 620 | { "afe4404", 0 }, |
| 621 | { /* sentinel */ } |
| 622 | }; |
| 623 | MODULE_DEVICE_TABLE(i2c, afe4404_ids); |
| 624 | |
| 625 | static struct i2c_driver afe4404_i2c_driver = { |
| 626 | .driver = { |
| 627 | .name = AFE4404_DRIVER_NAME, |
| 628 | .of_match_table = afe4404_of_match, |
| 629 | .pm = &afe4404_pm_ops, |
| 630 | }, |
| 631 | .probe = afe4404_probe, |
| 632 | .remove = afe4404_remove, |
| 633 | .id_table = afe4404_ids, |
| 634 | }; |
| 635 | module_i2c_driver(afe4404_i2c_driver); |
| 636 | |
| 637 | MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>"); |
| 638 | MODULE_DESCRIPTION("TI AFE4404 Heart Rate Monitor and Pulse Oximeter AFE"); |
| 639 | MODULE_LICENSE("GPL v2"); |