1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * isl28022.c - driver for Renesas ISL28022 power monitor chip monitoring
5 * Copyright (c) 2023 Carsten Spieß <mail@carsten-spiess.de>
8 #include <linux/debugfs.h>
10 #include <linux/hwmon.h>
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
15 /* ISL28022 registers */
16 #define ISL28022_REG_CONFIG 0x00
17 #define ISL28022_REG_SHUNT 0x01
18 #define ISL28022_REG_BUS 0x02
19 #define ISL28022_REG_POWER 0x03
20 #define ISL28022_REG_CURRENT 0x04
21 #define ISL28022_REG_CALIB 0x05
22 #define ISL28022_REG_SHUNT_THR 0x06
23 #define ISL28022_REG_BUS_THR 0x07
24 #define ISL28022_REG_INT 0x08
25 #define ISL28022_REG_AUX 0x09
26 #define ISL28022_REG_MAX ISL28022_REG_AUX
28 /* ISL28022 config flags */
30 #define ISL28022_MODE_SHIFT 0
31 #define ISL28022_MODE_MASK 0x0007
33 #define ISL28022_MODE_PWR_DOWN 0x0
34 #define ISL28022_MODE_TRG_S 0x1
35 #define ISL28022_MODE_TRG_B 0x2
36 #define ISL28022_MODE_TRG_SB 0x3
37 #define ISL28022_MODE_ADC_OFF 0x4
38 #define ISL28022_MODE_CONT_S 0x5
39 #define ISL28022_MODE_CONT_B 0x6
40 #define ISL28022_MODE_CONT_SB 0x7
42 /* shunt ADC settings */
43 #define ISL28022_SADC_SHIFT 3
44 #define ISL28022_SADC_MASK 0x0078
46 #define ISL28022_BADC_SHIFT 7
47 #define ISL28022_BADC_MASK 0x0780
49 #define ISL28022_ADC_12 0x0 /* 12 bit ADC */
50 #define ISL28022_ADC_13 0x1 /* 13 bit ADC */
51 #define ISL28022_ADC_14 0x2 /* 14 bit ADC */
52 #define ISL28022_ADC_15 0x3 /* 15 bit ADC */
53 #define ISL28022_ADC_15_1 0x8 /* 15 bit ADC, 1 sample */
54 #define ISL28022_ADC_15_2 0x9 /* 15 bit ADC, 2 samples */
55 #define ISL28022_ADC_15_4 0xA /* 15 bit ADC, 4 samples */
56 #define ISL28022_ADC_15_8 0xB /* 15 bit ADC, 8 samples */
57 #define ISL28022_ADC_15_16 0xC /* 15 bit ADC, 16 samples */
58 #define ISL28022_ADC_15_32 0xD /* 15 bit ADC, 32 samples */
59 #define ISL28022_ADC_15_64 0xE /* 15 bit ADC, 64 samples */
60 #define ISL28022_ADC_15_128 0xF /* 15 bit ADC, 128 samples */
62 /* shunt voltage range */
63 #define ISL28022_PG_SHIFT 11
64 #define ISL28022_PG_MASK 0x1800
66 #define ISL28022_PG_40 0x0 /* +/-40 mV */
67 #define ISL28022_PG_80 0x1 /* +/-80 mV */
68 #define ISL28022_PG_160 0x2 /* +/-160 mV */
69 #define ISL28022_PG_320 0x3 /* +/-3200 mV */
71 /* bus voltage range */
72 #define ISL28022_BRNG_SHIFT 13
73 #define ISL28022_BRNG_MASK 0x6000
75 #define ISL28022_BRNG_16 0x0 /* 16 V */
76 #define ISL28022_BRNG_32 0x1 /* 32 V */
77 #define ISL28022_BRNG_60 0x3 /* 60 V */
80 #define ISL28022_RESET 0x8000
82 struct isl28022_data
{
83 struct regmap
*regmap
;
89 static int isl28022_read_in(struct device
*dev
, u32 attr
, int channel
, long *val
)
91 struct isl28022_data
*data
= dev_get_drvdata(dev
);
100 err
= regmap_read(data
->regmap
,
101 ISL28022_REG_BUS
, ®val
);
104 /* driver supports only 60V mode (BRNG 11) */
105 *val
= (long)(((u16
)regval
) & 0xFFFC);
114 err
= regmap_read(data
->regmap
,
115 ISL28022_REG_SHUNT
, ®val
);
118 switch (data
->gain
) {
120 sign_bit
= (regval
>> 15) & 0x01;
121 *val
= (long)((((u16
)regval
) & 0x7FFF) -
122 (sign_bit
* 32768)) / 100;
125 sign_bit
= (regval
>> 14) & 0x01;
126 *val
= (long)((((u16
)regval
) & 0x3FFF) -
127 (sign_bit
* 16384)) / 100;
130 sign_bit
= (regval
>> 13) & 0x01;
131 *val
= (long)((((u16
)regval
) & 0x1FFF) -
132 (sign_bit
* 8192)) / 100;
135 sign_bit
= (regval
>> 12) & 0x01;
136 *val
= (long)((((u16
)regval
) & 0x0FFF) -
137 (sign_bit
* 4096)) / 100;
152 static int isl28022_read_current(struct device
*dev
, u32 attr
, long *val
)
154 struct isl28022_data
*data
= dev_get_drvdata(dev
);
159 case hwmon_curr_input
:
160 err
= regmap_read(data
->regmap
,
161 ISL28022_REG_CURRENT
, ®val
);
164 *val
= ((long)regval
* 1250L * (long)data
->gain
) /
174 static int isl28022_read_power(struct device
*dev
, u32 attr
, long *val
)
176 struct isl28022_data
*data
= dev_get_drvdata(dev
);
181 case hwmon_power_input
:
182 err
= regmap_read(data
->regmap
,
183 ISL28022_REG_POWER
, ®val
);
186 *val
= ((51200000L * ((long)data
->gain
)) /
187 (long)data
->shunt
) * (long)regval
;
196 static int isl28022_read(struct device
*dev
, enum hwmon_sensor_types type
,
197 u32 attr
, int channel
, long *val
)
201 return isl28022_read_in(dev
, attr
, channel
, val
);
203 return isl28022_read_current(dev
, attr
, val
);
205 return isl28022_read_power(dev
, attr
, val
);
212 static umode_t
isl28022_is_visible(const void *data
, enum hwmon_sensor_types type
,
213 u32 attr
, int channel
)
226 case hwmon_curr_input
:
234 case hwmon_power_input
:
246 static const struct hwmon_channel_info
*isl28022_info
[] = {
247 HWMON_CHANNEL_INFO(in
,
248 HWMON_I_INPUT
, /* channel 0: bus voltage (mV) */
249 HWMON_I_INPUT
), /* channel 1: shunt voltage (mV) */
250 HWMON_CHANNEL_INFO(curr
,
251 HWMON_C_INPUT
), /* channel 1: current (mA) */
252 HWMON_CHANNEL_INFO(power
,
253 HWMON_P_INPUT
), /* channel 1: power (µW) */
257 static const struct hwmon_ops isl28022_hwmon_ops
= {
258 .is_visible
= isl28022_is_visible
,
259 .read
= isl28022_read
,
262 static const struct hwmon_chip_info isl28022_chip_info
= {
263 .ops
= &isl28022_hwmon_ops
,
264 .info
= isl28022_info
,
267 static bool isl28022_is_writeable_reg(struct device
*dev
, unsigned int reg
)
270 case ISL28022_REG_CONFIG
:
271 case ISL28022_REG_CALIB
:
272 case ISL28022_REG_SHUNT_THR
:
273 case ISL28022_REG_BUS_THR
:
274 case ISL28022_REG_INT
:
275 case ISL28022_REG_AUX
:
282 static bool isl28022_is_volatile_reg(struct device
*dev
, unsigned int reg
)
285 case ISL28022_REG_CONFIG
:
286 case ISL28022_REG_SHUNT
:
287 case ISL28022_REG_BUS
:
288 case ISL28022_REG_POWER
:
289 case ISL28022_REG_CURRENT
:
290 case ISL28022_REG_INT
:
291 case ISL28022_REG_AUX
:
297 static const struct regmap_config isl28022_regmap_config
= {
300 .max_register
= ISL28022_REG_MAX
,
301 .writeable_reg
= isl28022_is_writeable_reg
,
302 .volatile_reg
= isl28022_is_volatile_reg
,
303 .val_format_endian
= REGMAP_ENDIAN_BIG
,
304 .cache_type
= REGCACHE_RBTREE
,
305 .use_single_read
= true,
306 .use_single_write
= true,
309 static int shunt_voltage_show(struct seq_file
*seqf
, void *unused
)
311 struct isl28022_data
*data
= seqf
->private;
315 err
= regmap_read(data
->regmap
,
316 ISL28022_REG_SHUNT
, ®val
);
320 /* print shunt voltage in micro volt */
321 seq_printf(seqf
, "%d\n", regval
* 10);
325 DEFINE_SHOW_ATTRIBUTE(shunt_voltage
);
327 static struct dentry
*isl28022_debugfs_root
;
329 static void isl28022_debugfs_remove(void *res
)
331 debugfs_remove_recursive(res
);
334 static void isl28022_debugfs_init(struct i2c_client
*client
, struct isl28022_data
*data
)
337 struct dentry
*debugfs
;
339 scnprintf(name
, sizeof(name
), "%d-%04hx", client
->adapter
->nr
, client
->addr
);
341 debugfs
= debugfs_create_dir(name
, isl28022_debugfs_root
);
342 debugfs_create_file("shunt_voltage", 0444, debugfs
, data
, &shunt_voltage_fops
);
344 devm_add_action_or_reset(&client
->dev
, isl28022_debugfs_remove
, debugfs
);
348 * read property values and make consistency checks.
350 * following values for shunt range and resistor are allowed:
351 * 40 mV -> gain 1, shunt min. 800 micro ohms
352 * 80 mV -> gain 2, shunt min. 1600 micro ohms
353 * 160 mV -> gain 4, shunt min. 3200 micro ohms
354 * 320 mV -> gain 8, shunt min. 6400 micro ohms
356 static int isl28022_read_properties(struct device
*dev
, struct isl28022_data
*data
)
361 err
= device_property_read_u32(dev
, "shunt-resistor-micro-ohms", &val
);
368 err
= device_property_read_u32(dev
, "renesas,shunt-range-microvolt", &val
);
377 if (data
->shunt
< 800)
382 if (data
->shunt
< 1600)
387 if (data
->shunt
< 3200)
392 if (data
->shunt
< 6400)
396 return dev_err_probe(dev
, -EINVAL
,
397 "renesas,shunt-range-microvolt invalid value %d\n",
401 err
= device_property_read_u32(dev
, "renesas,average-samples", &val
);
406 if (val
> 128 || hweight32(val
) != 1)
407 return dev_err_probe(dev
, -EINVAL
,
408 "renesas,average-samples invalid value %d\n",
416 return dev_err_probe(dev
, -EINVAL
,
417 "renesas,shunt-resistor-microvolt invalid value %d\n",
422 * write configuration and calibration registers
424 * The driver supports only shunt and bus continuous ADC mode at 15bit resolution
425 * with averaging from 1 to 128 samples (pow of 2) on both channels.
426 * Shunt voltage gain 1,2,4 or 8 is allowed.
427 * The bus voltage range is 60V fixed.
429 static int isl28022_config(struct isl28022_data
*data
)
435 config
= (ISL28022_MODE_CONT_SB
<< ISL28022_MODE_SHIFT
) |
436 (ISL28022_BRNG_60
<< ISL28022_BRNG_SHIFT
) |
437 (__ffs(data
->gain
) << ISL28022_PG_SHIFT
) |
438 ((ISL28022_ADC_15_1
+ __ffs(data
->average
)) << ISL28022_SADC_SHIFT
) |
439 ((ISL28022_ADC_15_1
+ __ffs(data
->average
)) << ISL28022_BADC_SHIFT
);
441 calib
= data
->shunt
? 0x8000 / data
->gain
: 0;
443 err
= regmap_write(data
->regmap
, ISL28022_REG_CONFIG
, config
);
447 return regmap_write(data
->regmap
, ISL28022_REG_CALIB
, calib
);
450 static int isl28022_probe(struct i2c_client
*client
)
452 struct device
*dev
= &client
->dev
;
453 struct device
*hwmon_dev
;
454 struct isl28022_data
*data
;
457 if (!i2c_check_functionality(client
->adapter
,
458 I2C_FUNC_SMBUS_BYTE_DATA
|
459 I2C_FUNC_SMBUS_WORD_DATA
))
462 data
= devm_kzalloc(dev
, sizeof(struct isl28022_data
), GFP_KERNEL
);
466 err
= isl28022_read_properties(dev
, data
);
470 data
->regmap
= devm_regmap_init_i2c(client
, &isl28022_regmap_config
);
471 if (IS_ERR(data
->regmap
))
472 return PTR_ERR(data
->regmap
);
474 err
= isl28022_config(data
);
478 isl28022_debugfs_init(client
, data
);
480 hwmon_dev
= devm_hwmon_device_register_with_info(dev
, client
->name
,
481 data
, &isl28022_chip_info
, NULL
);
482 if (IS_ERR(hwmon_dev
))
483 return PTR_ERR(hwmon_dev
);
488 static const struct i2c_device_id isl28022_ids
[] = {
492 MODULE_DEVICE_TABLE(i2c
, isl28022_ids
);
494 static const struct of_device_id __maybe_unused isl28022_of_match
[] = {
495 { .compatible
= "renesas,isl28022"},
498 MODULE_DEVICE_TABLE(of
, isl28022_of_match
);
500 static struct i2c_driver isl28022_driver
= {
501 .class = I2C_CLASS_HWMON
,
505 .probe
= isl28022_probe
,
506 .id_table
= isl28022_ids
,
514 isl28022_debugfs_root
= debugfs_create_dir("isl28022", NULL
);
515 err
= i2c_add_driver(&isl28022_driver
);
519 debugfs_remove_recursive(isl28022_debugfs_root
);
526 i2c_del_driver(&isl28022_driver
);
527 debugfs_remove_recursive(isl28022_debugfs_root
);
530 module_init(isl28022_init
);
531 module_exit(isl28022_exit
);
533 MODULE_AUTHOR("Carsten Spieß <mail@carsten-spiess.de>");
534 MODULE_DESCRIPTION("ISL28022 driver");
535 MODULE_LICENSE("GPL");