1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * HMC425A and similar Gain Amplifiers 4 * 5 * Copyright 2020, 2024 Analog Devices Inc. 6 */ 7 8 #include <linux/bits.h> 9 #include <linux/bitops.h> 10 #include <linux/device.h> 11 #include <linux/err.h> 12 #include <linux/gpio/consumer.h> 13 #include <linux/iio/iio.h> 14 #include <linux/iio/sysfs.h> 15 #include <linux/kernel.h> 16 #include <linux/math.h> 17 #include <linux/mod_devicetable.h> 18 #include <linux/module.h> 19 #include <linux/platform_device.h> 20 #include <linux/property.h> 21 #include <linux/slab.h> 22 #include <linux/regulator/consumer.h> 23 #include <linux/sysfs.h> 24 25 /* 26 * The LTC6373 amplifier supports configuring gain using GPIO's with the following 27 * values (OUTPUT_V / INPUT_V): 0(shutdown), 0.25, 0.5, 1, 2, 4, 8, 16 28 * 29 * Except for the shutdown value, all can be converted to dB using 20 * log10(x) 30 * From here, it is observed that all values are multiples of the '2' gain setting, 31 * with the correspondent of 6.020dB. 32 */ 33 #define LTC6373_CONVERSION_CONSTANT 6020 34 #define LTC6373_MIN_GAIN_CODE 0x6 35 #define LTC6373_CONVERSION_MASK GENMASK(2, 0) 36 #define LTC6373_SHUTDOWN GENMASK(2, 0) 37 38 enum hmc425a_type { 39 ID_HMC425A, 40 ID_HMC540S, 41 ID_ADRF5740, 42 ID_LTC6373, 43 }; 44 45 struct hmc425a_chip_info { 46 const char *name; 47 const struct iio_chan_spec *channels; 48 unsigned int num_channels; 49 unsigned int num_gpios; 50 int gain_min; 51 int gain_max; 52 int default_gain; 53 int powerdown_val; 54 bool has_powerdown; 55 56 int (*gain_dB_to_code)(int gain, int *code); 57 int (*code_to_gain_dB)(int code, int *val, int *val2); 58 }; 59 60 struct hmc425a_state { 61 struct mutex lock; /* protect sensor state */ 62 const struct hmc425a_chip_info *chip_info; 63 struct gpio_descs *gpios; 64 u32 gain; 65 bool powerdown; 66 }; 67 68 static int gain_dB_to_code(struct hmc425a_state *st, int val, int val2, int *code) 69 { 70 const struct hmc425a_chip_info *inf = st->chip_info; 71 int gain; 72 73 if (val < 0) 74 gain = (val * 1000) - (val2 / 1000); 75 else 76 gain = (val * 1000) + (val2 / 1000); 77 78 if (gain > inf->gain_max || gain < inf->gain_min) 79 return -EINVAL; 80 if (st->powerdown) 81 return -EPERM; 82 83 return st->chip_info->gain_dB_to_code(gain, code); 84 } 85 86 static int hmc425a_gain_dB_to_code(int gain, int *code) 87 { 88 *code = ~((abs(gain) / 500) & 0x3F); 89 return 0; 90 } 91 92 static int hmc540s_gain_dB_to_code(int gain, int *code) 93 { 94 *code = ~((abs(gain) / 1000) & 0xF); 95 return 0; 96 } 97 98 static int adrf5740_gain_dB_to_code(int gain, int *code) 99 { 100 int temp = (abs(gain) / 2000) & 0xF; 101 102 /* Bit [0-3]: 2dB 4dB 8dB 8dB */ 103 *code = temp & BIT(3) ? temp | BIT(2) : temp; 104 return 0; 105 } 106 107 static int ltc6373_gain_dB_to_code(int gain, int *code) 108 { 109 *code = ~(DIV_ROUND_CLOSEST(gain, LTC6373_CONVERSION_CONSTANT) + 3) 110 & LTC6373_CONVERSION_MASK; 111 return 0; 112 } 113 114 static int code_to_gain_dB(struct hmc425a_state *st, int *val, int *val2) 115 { 116 if (st->powerdown) 117 return -EPERM; 118 return st->chip_info->code_to_gain_dB(st->gain, val, val2); 119 } 120 121 static int hmc425a_code_to_gain_dB(int code, int *val, int *val2) 122 { 123 *val = (~code * -500) / 1000; 124 *val2 = ((~code * -500) % 1000) * 1000; 125 return 0; 126 } 127 128 static int hmc540s_code_to_gain_dB(int code, int *val, int *val2) 129 { 130 *val = (~code * -1000) / 1000; 131 *val2 = ((~code * -1000) % 1000) * 1000; 132 return 0; 133 } 134 135 static int adrf5740_code_to_gain_dB(int code, int *val, int *val2) 136 { 137 /* 138 * Bit [0-3]: 2dB 4dB 8dB 8dB 139 * When BIT(3) is set, unset BIT(2) and use 3 as double the place value 140 */ 141 code = code & BIT(3) ? code & ~BIT(2) : code; 142 *val = (code * -2000) / 1000; 143 *val2 = ((code * -2000) % 1000) * 1000; 144 return 0; 145 } 146 147 static int ltc6373_code_to_gain_dB(int code, int *val, int *val2) 148 { 149 int gain = ((~code & LTC6373_CONVERSION_MASK) - 3) * 150 LTC6373_CONVERSION_CONSTANT; 151 152 *val = gain / 1000; 153 *val2 = (gain % 1000) * 1000; 154 return 0; 155 } 156 157 static int hmc425a_write(struct iio_dev *indio_dev, u32 value) 158 { 159 struct hmc425a_state *st = iio_priv(indio_dev); 160 DECLARE_BITMAP(values, BITS_PER_TYPE(value)); 161 162 values[0] = value; 163 164 gpiod_multi_set_value_cansleep(st->gpios, values); 165 return 0; 166 } 167 168 static int hmc425a_read_raw(struct iio_dev *indio_dev, 169 struct iio_chan_spec const *chan, int *val, 170 int *val2, long m) 171 { 172 struct hmc425a_state *st = iio_priv(indio_dev); 173 int ret; 174 175 mutex_lock(&st->lock); 176 switch (m) { 177 case IIO_CHAN_INFO_HARDWAREGAIN: 178 ret = code_to_gain_dB(st, val, val2); 179 if (ret) 180 break; 181 ret = IIO_VAL_INT_PLUS_MICRO_DB; 182 break; 183 default: 184 ret = -EINVAL; 185 } 186 mutex_unlock(&st->lock); 187 188 return ret; 189 }; 190 191 static int hmc425a_write_raw(struct iio_dev *indio_dev, 192 struct iio_chan_spec const *chan, int val, 193 int val2, long mask) 194 { 195 struct hmc425a_state *st = iio_priv(indio_dev); 196 int code = 0, ret; 197 198 mutex_lock(&st->lock); 199 switch (mask) { 200 case IIO_CHAN_INFO_HARDWAREGAIN: 201 ret = gain_dB_to_code(st, val, val2, &code); 202 if (ret) 203 break; 204 st->gain = code; 205 206 ret = hmc425a_write(indio_dev, st->gain); 207 break; 208 default: 209 ret = -EINVAL; 210 } 211 mutex_unlock(&st->lock); 212 213 return ret; 214 } 215 216 static int hmc425a_write_raw_get_fmt(struct iio_dev *indio_dev, 217 struct iio_chan_spec const *chan, 218 long mask) 219 { 220 switch (mask) { 221 case IIO_CHAN_INFO_HARDWAREGAIN: 222 return IIO_VAL_INT_PLUS_MICRO_DB; 223 default: 224 return -EINVAL; 225 } 226 } 227 228 static const struct iio_info hmc425a_info = { 229 .read_raw = &hmc425a_read_raw, 230 .write_raw = &hmc425a_write_raw, 231 .write_raw_get_fmt = &hmc425a_write_raw_get_fmt, 232 }; 233 234 static ssize_t ltc6373_read_powerdown(struct iio_dev *indio_dev, 235 uintptr_t private, 236 const struct iio_chan_spec *chan, 237 char *buf) 238 { 239 struct hmc425a_state *st = iio_priv(indio_dev); 240 241 return sysfs_emit(buf, "%d\n", st->powerdown); 242 } 243 244 static ssize_t ltc6373_write_powerdown(struct iio_dev *indio_dev, 245 uintptr_t private, 246 const struct iio_chan_spec *chan, 247 const char *buf, 248 size_t len) 249 { 250 struct hmc425a_state *st = iio_priv(indio_dev); 251 bool powerdown; 252 int code, ret; 253 254 ret = kstrtobool(buf, &powerdown); 255 if (ret) 256 return ret; 257 258 mutex_lock(&st->lock); 259 st->powerdown = powerdown; 260 code = (powerdown) ? LTC6373_SHUTDOWN : st->gain; 261 hmc425a_write(indio_dev, code); 262 mutex_unlock(&st->lock); 263 return len; 264 } 265 266 static const struct iio_chan_spec_ext_info ltc6373_ext_info[] = { 267 { 268 .name = "powerdown", 269 .read = ltc6373_read_powerdown, 270 .write = ltc6373_write_powerdown, 271 .shared = IIO_SEPARATE, 272 }, 273 { } 274 }; 275 276 #define HMC425A_CHAN(_channel) \ 277 { \ 278 .type = IIO_VOLTAGE, \ 279 .output = 1, \ 280 .indexed = 1, \ 281 .channel = _channel, \ 282 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN), \ 283 } 284 285 #define LTC6373_CHAN(_channel) \ 286 { \ 287 .type = IIO_VOLTAGE, \ 288 .output = 1, \ 289 .indexed = 1, \ 290 .channel = _channel, \ 291 .info_mask_separate = BIT(IIO_CHAN_INFO_HARDWAREGAIN), \ 292 .ext_info = ltc6373_ext_info, \ 293 } 294 295 static const struct iio_chan_spec hmc425a_channels[] = { 296 HMC425A_CHAN(0), 297 }; 298 299 static const struct iio_chan_spec ltc6373_channels[] = { 300 LTC6373_CHAN(0), 301 }; 302 303 static const struct hmc425a_chip_info hmc425a_chip_info_tbl[] = { 304 [ID_HMC425A] = { 305 .name = "hmc425a", 306 .channels = hmc425a_channels, 307 .num_channels = ARRAY_SIZE(hmc425a_channels), 308 .num_gpios = 6, 309 .gain_min = -31500, 310 .gain_max = 0, 311 .default_gain = -0x40, /* set default gain -31.5db*/ 312 .gain_dB_to_code = hmc425a_gain_dB_to_code, 313 .code_to_gain_dB = hmc425a_code_to_gain_dB, 314 }, 315 [ID_HMC540S] = { 316 .name = "hmc540s", 317 .channels = hmc425a_channels, 318 .num_channels = ARRAY_SIZE(hmc425a_channels), 319 .num_gpios = 4, 320 .gain_min = -15000, 321 .gain_max = 0, 322 .default_gain = -0x10, /* set default gain -15.0db*/ 323 .gain_dB_to_code = hmc540s_gain_dB_to_code, 324 .code_to_gain_dB = hmc540s_code_to_gain_dB, 325 }, 326 [ID_ADRF5740] = { 327 .name = "adrf5740", 328 .channels = hmc425a_channels, 329 .num_channels = ARRAY_SIZE(hmc425a_channels), 330 .num_gpios = 4, 331 .gain_min = -22000, 332 .gain_max = 0, 333 .default_gain = 0xF, /* set default gain -22.0db*/ 334 .gain_dB_to_code = adrf5740_gain_dB_to_code, 335 .code_to_gain_dB = adrf5740_code_to_gain_dB, 336 }, 337 [ID_LTC6373] = { 338 .name = "ltc6373", 339 .channels = ltc6373_channels, 340 .num_channels = ARRAY_SIZE(ltc6373_channels), 341 .num_gpios = 3, 342 .gain_min = -12041, /* gain setting x0.25*/ 343 .gain_max = 24082, /* gain setting x16 */ 344 .default_gain = LTC6373_MIN_GAIN_CODE, 345 .powerdown_val = LTC6373_SHUTDOWN, 346 .has_powerdown = true, 347 .gain_dB_to_code = ltc6373_gain_dB_to_code, 348 .code_to_gain_dB = ltc6373_code_to_gain_dB, 349 }, 350 }; 351 352 static int hmc425a_probe(struct platform_device *pdev) 353 { 354 struct iio_dev *indio_dev; 355 struct hmc425a_state *st; 356 int ret; 357 358 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*st)); 359 if (!indio_dev) 360 return -ENOMEM; 361 362 st = iio_priv(indio_dev); 363 364 st->chip_info = device_get_match_data(&pdev->dev); 365 indio_dev->num_channels = st->chip_info->num_channels; 366 indio_dev->channels = st->chip_info->channels; 367 indio_dev->name = st->chip_info->name; 368 st->gain = st->chip_info->default_gain; 369 370 st->gpios = devm_gpiod_get_array(&pdev->dev, "ctrl", GPIOD_OUT_LOW); 371 if (IS_ERR(st->gpios)) 372 return dev_err_probe(&pdev->dev, PTR_ERR(st->gpios), 373 "failed to get gpios\n"); 374 375 if (st->gpios->ndescs != st->chip_info->num_gpios) { 376 dev_err(&pdev->dev, "%d GPIOs needed to operate\n", 377 st->chip_info->num_gpios); 378 return -ENODEV; 379 } 380 381 ret = devm_regulator_get_enable(&pdev->dev, "vcc-supply"); 382 if (ret) 383 return ret; 384 385 mutex_init(&st->lock); 386 387 indio_dev->info = &hmc425a_info; 388 indio_dev->modes = INDIO_DIRECT_MODE; 389 390 if (st->chip_info->has_powerdown) { 391 st->powerdown = true; 392 hmc425a_write(indio_dev, st->chip_info->powerdown_val); 393 } else { 394 /* Set default gain */ 395 hmc425a_write(indio_dev, st->gain); 396 } 397 398 return devm_iio_device_register(&pdev->dev, indio_dev); 399 } 400 401 static const struct of_device_id hmc425a_of_match[] = { 402 { .compatible = "adi,hmc425a", 403 .data = &hmc425a_chip_info_tbl[ID_HMC425A]}, 404 { .compatible = "adi,hmc540s", 405 .data = &hmc425a_chip_info_tbl[ID_HMC540S]}, 406 { .compatible = "adi,adrf5740", 407 .data = &hmc425a_chip_info_tbl[ID_ADRF5740]}, 408 { .compatible = "adi,ltc6373", 409 .data = &hmc425a_chip_info_tbl[ID_LTC6373]}, 410 { } 411 }; 412 MODULE_DEVICE_TABLE(of, hmc425a_of_match); 413 414 static struct platform_driver hmc425a_driver = { 415 .driver = { 416 .name = KBUILD_MODNAME, 417 .of_match_table = hmc425a_of_match, 418 }, 419 .probe = hmc425a_probe, 420 }; 421 module_platform_driver(hmc425a_driver); 422 423 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>"); 424 MODULE_DESCRIPTION("Analog Devices HMC425A and similar GPIO control Gain Amplifiers"); 425 MODULE_LICENSE("GPL v2"); 426