Lines Matching +full:reg +full:- +full:data

1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (c) 2009-2010 Analog Devices, Inc.
33 * AD5291 1 256 20, 50, 100 (20-TP)
34 * AD5292 1 1024 20, 50, 100 (20-TP)
56 * AD5270 1 1024 20, 50, 100 (50-TP)
57 * AD5271 1 256 20, 50, 100 (50-TP)
58 * AD5272 1 1024 20, 50, 100 (50-TP)
59 * AD5274 1 256 20, 50, 100 (50-TP)
61 * See Documentation/misc-devices/ad525x_dpot.rst for more info.
68 * Copyright (c) 2006-2011 Michael Hennerich <michael.hennerich@analog.com>
80 * Client data (each client gets its own)
98 return dpot->bdata.bops->read_d8(dpot->bdata.client); in dpot_read_d8()
101 static inline int dpot_read_r8d8(struct dpot_data *dpot, u8 reg) in dpot_read_r8d8() argument
103 return dpot->bdata.bops->read_r8d8(dpot->bdata.client, reg); in dpot_read_r8d8()
106 static inline int dpot_read_r8d16(struct dpot_data *dpot, u8 reg) in dpot_read_r8d16() argument
108 return dpot->bdata.bops->read_r8d16(dpot->bdata.client, reg); in dpot_read_r8d16()
113 return dpot->bdata.bops->write_d8(dpot->bdata.client, val); in dpot_write_d8()
116 static inline int dpot_write_r8d8(struct dpot_data *dpot, u8 reg, u16 val) in dpot_write_r8d8() argument
118 return dpot->bdata.bops->write_r8d8(dpot->bdata.client, reg, val); in dpot_write_r8d8()
121 static inline int dpot_write_r8d16(struct dpot_data *dpot, u8 reg, u16 val) in dpot_write_r8d16() argument
123 return dpot->bdata.bops->write_r8d16(dpot->bdata.client, reg, val); in dpot_write_r8d16()
126 static s32 dpot_read_spi(struct dpot_data *dpot, u8 reg) in dpot_read_spi() argument
131 if (!(reg & (DPOT_ADDR_EEPROM | DPOT_ADDR_CMD))) { in dpot_read_spi()
133 if (dpot->feat & F_RDACS_WONLY) in dpot_read_spi()
134 return dpot->rdac_cache[reg & DPOT_RDAC_MASK]; in dpot_read_spi()
135 if (dpot->uid == DPOT_UID(AD5291_ID) || in dpot_read_spi()
136 dpot->uid == DPOT_UID(AD5292_ID) || in dpot_read_spi()
137 dpot->uid == DPOT_UID(AD5293_ID)) { in dpot_read_spi()
145 if (dpot->uid == DPOT_UID(AD5291_ID)) in dpot_read_spi()
149 } else if (dpot->uid == DPOT_UID(AD5270_ID) || in dpot_read_spi()
150 dpot->uid == DPOT_UID(AD5271_ID)) { in dpot_read_spi()
158 if (dpot->uid == DPOT_UID(AD5271_ID)) in dpot_read_spi()
165 } else if (reg & DPOT_ADDR_EEPROM) { in dpot_read_spi()
169 if (dpot->feat & F_SPI_16BIT) in dpot_read_spi()
171 else if (dpot->feat & F_SPI_24BIT) in dpot_read_spi()
174 return -EFAULT; in dpot_read_spi()
177 static s32 dpot_read_i2c(struct dpot_data *dpot, u8 reg) in dpot_read_i2c() argument
182 switch (dpot->uid) { in dpot_read_i2c()
193 ctrl = ((reg & DPOT_RDAC_MASK) == DPOT_RDAC0) ? in dpot_read_i2c()
202 ctrl = ((reg & DPOT_RDAC_MASK) == DPOT_RDAC0) ? in dpot_read_i2c()
219 if (dpot->uid == DPOT_UID(AD5274_ID)) in dpot_read_i2c()
223 if ((reg & DPOT_REG_TOL) || (dpot->max_pos > 256)) in dpot_read_i2c()
224 return dpot_read_r8d16(dpot, (reg & 0xF8) | in dpot_read_i2c()
225 ((reg & 0x7) << 1)); in dpot_read_i2c()
227 return dpot_read_r8d8(dpot, reg); in dpot_read_i2c()
231 static s32 dpot_read(struct dpot_data *dpot, u8 reg) in dpot_read() argument
233 if (dpot->feat & F_SPI) in dpot_read()
234 return dpot_read_spi(dpot, reg); in dpot_read()
236 return dpot_read_i2c(dpot, reg); in dpot_read()
239 static s32 dpot_write_spi(struct dpot_data *dpot, u8 reg, u16 value) in dpot_write_spi() argument
243 if (!(reg & (DPOT_ADDR_EEPROM | DPOT_ADDR_CMD | DPOT_ADDR_OTP))) { in dpot_write_spi()
244 if (dpot->feat & F_RDACS_WONLY) in dpot_write_spi()
245 dpot->rdac_cache[reg & DPOT_RDAC_MASK] = value; in dpot_write_spi()
247 if (dpot->feat & F_AD_APPDATA) { in dpot_write_spi()
248 if (dpot->feat & F_SPI_8BIT) { in dpot_write_spi()
249 val = ((reg & DPOT_RDAC_MASK) << in dpot_write_spi()
250 DPOT_MAX_POS(dpot->devid)) | in dpot_write_spi()
253 } else if (dpot->feat & F_SPI_16BIT) { in dpot_write_spi()
254 val = ((reg & DPOT_RDAC_MASK) << in dpot_write_spi()
255 DPOT_MAX_POS(dpot->devid)) | in dpot_write_spi()
262 if (dpot->uid == DPOT_UID(AD5291_ID) || in dpot_write_spi()
263 dpot->uid == DPOT_UID(AD5292_ID) || in dpot_write_spi()
264 dpot->uid == DPOT_UID(AD5293_ID)) { in dpot_write_spi()
269 if (dpot->uid == DPOT_UID(AD5291_ID)) in dpot_write_spi()
275 } else if (dpot->uid == DPOT_UID(AD5270_ID) || in dpot_write_spi()
276 dpot->uid == DPOT_UID(AD5271_ID)) { in dpot_write_spi()
281 if (dpot->uid == DPOT_UID(AD5271_ID)) in dpot_write_spi()
288 val = DPOT_SPI_RDAC | (reg & DPOT_RDAC_MASK); in dpot_write_spi()
290 } else if (reg & DPOT_ADDR_EEPROM) { in dpot_write_spi()
291 val = DPOT_SPI_EEPROM | (reg & DPOT_RDAC_MASK); in dpot_write_spi()
292 } else if (reg & DPOT_ADDR_CMD) { in dpot_write_spi()
293 switch (reg) { in dpot_write_spi()
307 } else if (reg & DPOT_ADDR_OTP) { in dpot_write_spi()
308 if (dpot->uid == DPOT_UID(AD5291_ID) || in dpot_write_spi()
309 dpot->uid == DPOT_UID(AD5292_ID)) { in dpot_write_spi()
312 } else if (dpot->uid == DPOT_UID(AD5270_ID) || in dpot_write_spi()
313 dpot->uid == DPOT_UID(AD5271_ID)) { in dpot_write_spi()
320 if (dpot->feat & F_SPI_16BIT) in dpot_write_spi()
322 else if (dpot->feat & F_SPI_24BIT) in dpot_write_spi()
325 return -EFAULT; in dpot_write_spi()
328 static s32 dpot_write_i2c(struct dpot_data *dpot, u8 reg, u16 value) in dpot_write_i2c() argument
333 switch (dpot->uid) { in dpot_write_i2c()
345 ctrl = ((reg & DPOT_RDAC_MASK) == DPOT_RDAC0) ? in dpot_write_i2c()
350 if (reg & DPOT_ADDR_OTP) { in dpot_write_i2c()
353 return -EFAULT; in dpot_write_i2c()
359 ctrl = ((reg & DPOT_RDAC_MASK) == DPOT_RDAC0) ? in dpot_write_i2c()
361 if (reg & DPOT_ADDR_OTP) { in dpot_write_i2c()
364 return -EFAULT; in dpot_write_i2c()
369 if (reg & DPOT_ADDR_OTP) { in dpot_write_i2c()
372 return -EFAULT; in dpot_write_i2c()
381 if (reg & DPOT_ADDR_OTP) in dpot_write_i2c()
385 if (dpot->uid == DPOT_UID(AD5274_ID)) in dpot_write_i2c()
391 if (reg & DPOT_ADDR_CMD) in dpot_write_i2c()
392 return dpot_write_d8(dpot, reg); in dpot_write_i2c()
394 if (dpot->max_pos > 256) in dpot_write_i2c()
395 return dpot_write_r8d16(dpot, (reg & 0xF8) | in dpot_write_i2c()
396 ((reg & 0x7) << 1), value); in dpot_write_i2c()
398 /* All other registers require instruction + data bytes */ in dpot_write_i2c()
399 return dpot_write_r8d8(dpot, reg, value); in dpot_write_i2c()
403 static s32 dpot_write(struct dpot_data *dpot, u8 reg, u16 value) in dpot_write() argument
405 if (dpot->feat & F_SPI) in dpot_write()
406 return dpot_write_spi(dpot, reg, value); in dpot_write()
408 return dpot_write_i2c(dpot, reg, value); in dpot_write()
415 char *buf, u32 reg) in sysfs_show_reg() argument
417 struct dpot_data *data = dev_get_drvdata(dev); in sysfs_show_reg() local
420 if (reg & DPOT_ADDR_OTP_EN) in sysfs_show_reg()
422 test_bit(DPOT_RDAC_MASK & reg, data->otp_en_mask) ? in sysfs_show_reg()
426 mutex_lock(&data->update_lock); in sysfs_show_reg()
427 value = dpot_read(data, reg); in sysfs_show_reg()
428 mutex_unlock(&data->update_lock); in sysfs_show_reg()
431 return -EINVAL; in sysfs_show_reg()
434 * the tolerance is a two-byte value where the MSB in sysfs_show_reg()
440 if (reg & DPOT_REG_TOL) in sysfs_show_reg()
443 return sprintf(buf, "%u\n", value & data->rdac_mask); in sysfs_show_reg()
448 const char *buf, size_t count, u32 reg) in sysfs_set_reg() argument
450 struct dpot_data *data = dev_get_drvdata(dev); in sysfs_set_reg() local
454 if (reg & DPOT_ADDR_OTP_EN) { in sysfs_set_reg()
456 set_bit(DPOT_RDAC_MASK & reg, data->otp_en_mask); in sysfs_set_reg()
458 clear_bit(DPOT_RDAC_MASK & reg, data->otp_en_mask); in sysfs_set_reg()
463 if ((reg & DPOT_ADDR_OTP) && in sysfs_set_reg()
464 !test_bit(DPOT_RDAC_MASK & reg, data->otp_en_mask)) in sysfs_set_reg()
465 return -EPERM; in sysfs_set_reg()
471 if (value > data->rdac_mask) in sysfs_set_reg()
472 value = data->rdac_mask; in sysfs_set_reg()
474 mutex_lock(&data->update_lock); in sysfs_set_reg()
475 dpot_write(data, reg, value); in sysfs_set_reg()
476 if (reg & DPOT_ADDR_EEPROM) in sysfs_set_reg()
478 else if (reg & DPOT_ADDR_OTP) in sysfs_set_reg()
480 mutex_unlock(&data->update_lock); in sysfs_set_reg()
487 const char *buf, size_t count, u32 reg) in sysfs_do_cmd() argument
489 struct dpot_data *data = dev_get_drvdata(dev); in sysfs_do_cmd() local
491 mutex_lock(&data->update_lock); in sysfs_do_cmd()
492 dpot_write(data, reg, 0); in sysfs_do_cmd()
493 mutex_unlock(&data->update_lock); in sysfs_do_cmd()
498 /* ------------------------------------------------------------------------- */
515 #define DPOT_DEVICE_SHOW_SET(name, reg) \ argument
516 DPOT_DEVICE_SHOW(name, reg) \
517 DPOT_DEVICE_SET(name, reg) \
520 #define DPOT_DEVICE_SHOW_ONLY(name, reg) \ argument
521 DPOT_DEVICE_SHOW(name, reg) \
610 /* ------------------------------------------------------------------------- */
641 int err = sysfs_create_file(&dev->kobj, in ad_dpot_add_files()
644 err |= sysfs_create_file(&dev->kobj, in ad_dpot_add_files()
647 err |= sysfs_create_file(&dev->kobj, in ad_dpot_add_files()
650 err |= sysfs_create_file(&dev->kobj, in ad_dpot_add_files()
652 err |= sysfs_create_file(&dev->kobj, in ad_dpot_add_files()
666 sysfs_remove_file(&dev->kobj, in ad_dpot_remove_files()
669 sysfs_remove_file(&dev->kobj, in ad_dpot_remove_files()
672 sysfs_remove_file(&dev->kobj, in ad_dpot_remove_files()
675 sysfs_remove_file(&dev->kobj, in ad_dpot_remove_files()
677 sysfs_remove_file(&dev->kobj, in ad_dpot_remove_files()
687 struct dpot_data *data; in ad_dpot_probe() local
690 data = kzalloc(sizeof(struct dpot_data), GFP_KERNEL); in ad_dpot_probe()
691 if (!data) { in ad_dpot_probe()
692 err = -ENOMEM; in ad_dpot_probe()
696 dev_set_drvdata(dev, data); in ad_dpot_probe()
697 mutex_init(&data->update_lock); in ad_dpot_probe()
699 data->bdata = *bdata; in ad_dpot_probe()
700 data->devid = devid; in ad_dpot_probe()
702 data->max_pos = 1 << DPOT_MAX_POS(devid); in ad_dpot_probe()
703 data->rdac_mask = data->max_pos - 1; in ad_dpot_probe()
704 data->feat = DPOT_FEAT(devid); in ad_dpot_probe()
705 data->uid = DPOT_UID(devid); in ad_dpot_probe()
706 data->wipers = DPOT_WIPERS(devid); in ad_dpot_probe()
709 if (data->wipers & (1 << i)) { in ad_dpot_probe()
710 err = ad_dpot_add_files(dev, data->feat, i); in ad_dpot_probe()
713 /* power-up midscale */ in ad_dpot_probe()
714 if (data->feat & F_RDACS_WONLY) in ad_dpot_probe()
715 data->rdac_cache[i] = data->max_pos / 2; in ad_dpot_probe()
718 if (data->feat & F_CMD_INC) in ad_dpot_probe()
719 err = sysfs_create_group(&dev->kobj, &ad525x_group_commands); in ad_dpot_probe()
726 dev_info(dev, "%s %d-Position Digital Potentiometer registered\n", in ad_dpot_probe()
727 name, data->max_pos); in ad_dpot_probe()
733 if (data->wipers & (1 << i)) in ad_dpot_probe()
734 ad_dpot_remove_files(dev, data->feat, i); in ad_dpot_probe()
737 kfree(data); in ad_dpot_probe()
748 struct dpot_data *data = dev_get_drvdata(dev); in ad_dpot_remove() local
752 if (data->wipers & (1 << i)) in ad_dpot_remove()
753 ad_dpot_remove_files(dev, data->feat, i); in ad_dpot_remove()
755 kfree(data); in ad_dpot_remove()