1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * 1-Wire implementation for Maxim Semiconductor
3*4882a593Smuzhiyun * MAX7211/MAX17215 stanalone fuel gauge chip
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2017 Radioavionica Corporation
6*4882a593Smuzhiyun * Author: Alex A. Mihaylov <minimumlaw@rambler.ru>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Use consistent with the GNU GPL is permitted,
9*4882a593Smuzhiyun * provided that this copyright notice is
10*4882a593Smuzhiyun * preserved in its entirety in all copies and derived works.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun */
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/slab.h>
16*4882a593Smuzhiyun #include <linux/w1.h>
17*4882a593Smuzhiyun #include <linux/regmap.h>
18*4882a593Smuzhiyun #include <linux/power_supply.h>
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #define W1_MAX1721X_FAMILY_ID 0x26
21*4882a593Smuzhiyun #define DEF_DEV_NAME_MAX17211 "MAX17211"
22*4882a593Smuzhiyun #define DEF_DEV_NAME_MAX17215 "MAX17215"
23*4882a593Smuzhiyun #define DEF_DEV_NAME_UNKNOWN "UNKNOWN"
24*4882a593Smuzhiyun #define DEF_MFG_NAME "MAXIM"
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #define PSY_MAX_NAME_LEN 32
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /* Number of valid register addresses in W1 mode */
29*4882a593Smuzhiyun #define MAX1721X_MAX_REG_NR 0x1EF
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /* Factory settings (nonvilatile registers) (W1 specific) */
32*4882a593Smuzhiyun #define MAX1721X_REG_NRSENSE 0x1CF /* RSense in 10^-5 Ohm */
33*4882a593Smuzhiyun /* Strings */
34*4882a593Smuzhiyun #define MAX1721X_REG_MFG_STR 0x1CC
35*4882a593Smuzhiyun #define MAX1721X_REG_MFG_NUMB 3
36*4882a593Smuzhiyun #define MAX1721X_REG_DEV_STR 0x1DB
37*4882a593Smuzhiyun #define MAX1721X_REG_DEV_NUMB 5
38*4882a593Smuzhiyun /* HEX Strings */
39*4882a593Smuzhiyun #define MAX1721X_REG_SER_HEX 0x1D8
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun /* MAX172XX Output Registers for W1 chips */
42*4882a593Smuzhiyun #define MAX172XX_REG_STATUS 0x000 /* status reg */
43*4882a593Smuzhiyun #define MAX172XX_BAT_PRESENT (1<<4) /* battery connected bit */
44*4882a593Smuzhiyun #define MAX172XX_REG_DEVNAME 0x021 /* chip config */
45*4882a593Smuzhiyun #define MAX172XX_DEV_MASK 0x000F /* chip type mask */
46*4882a593Smuzhiyun #define MAX172X1_DEV 0x0001
47*4882a593Smuzhiyun #define MAX172X5_DEV 0x0005
48*4882a593Smuzhiyun #define MAX172XX_REG_TEMP 0x008 /* Temperature */
49*4882a593Smuzhiyun #define MAX172XX_REG_BATT 0x0DA /* Battery voltage */
50*4882a593Smuzhiyun #define MAX172XX_REG_CURRENT 0x00A /* Actual current */
51*4882a593Smuzhiyun #define MAX172XX_REG_AVGCURRENT 0x00B /* Average current */
52*4882a593Smuzhiyun #define MAX172XX_REG_REPSOC 0x006 /* Percentage of charge */
53*4882a593Smuzhiyun #define MAX172XX_REG_DESIGNCAP 0x018 /* Design capacity */
54*4882a593Smuzhiyun #define MAX172XX_REG_REPCAP 0x005 /* Average capacity */
55*4882a593Smuzhiyun #define MAX172XX_REG_TTE 0x011 /* Time to empty */
56*4882a593Smuzhiyun #define MAX172XX_REG_TTF 0x020 /* Time to full */
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun struct max17211_device_info {
59*4882a593Smuzhiyun char name[PSY_MAX_NAME_LEN];
60*4882a593Smuzhiyun struct power_supply *bat;
61*4882a593Smuzhiyun struct power_supply_desc bat_desc;
62*4882a593Smuzhiyun struct device *w1_dev;
63*4882a593Smuzhiyun struct regmap *regmap;
64*4882a593Smuzhiyun /* battery design format */
65*4882a593Smuzhiyun unsigned int rsense; /* in tenths uOhm */
66*4882a593Smuzhiyun char DeviceName[2 * MAX1721X_REG_DEV_NUMB + 1];
67*4882a593Smuzhiyun char ManufacturerName[2 * MAX1721X_REG_MFG_NUMB + 1];
68*4882a593Smuzhiyun char SerialNumber[13]; /* see get_sn_str() later for comment */
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /* Convert regs value to power_supply units */
72*4882a593Smuzhiyun
max172xx_time_to_ps(unsigned int reg)73*4882a593Smuzhiyun static inline int max172xx_time_to_ps(unsigned int reg)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun return reg * 5625 / 1000; /* in sec. */
76*4882a593Smuzhiyun }
77*4882a593Smuzhiyun
max172xx_percent_to_ps(unsigned int reg)78*4882a593Smuzhiyun static inline int max172xx_percent_to_ps(unsigned int reg)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun return reg / 256; /* in percent from 0 to 100 */
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
max172xx_voltage_to_ps(unsigned int reg)83*4882a593Smuzhiyun static inline int max172xx_voltage_to_ps(unsigned int reg)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun return reg * 1250; /* in uV */
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun
max172xx_capacity_to_ps(unsigned int reg)88*4882a593Smuzhiyun static inline int max172xx_capacity_to_ps(unsigned int reg)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun return reg * 500; /* in uAh */
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /*
94*4882a593Smuzhiyun * Current and temperature is signed values, so unsigned regs
95*4882a593Smuzhiyun * value must be converted to signed type
96*4882a593Smuzhiyun */
97*4882a593Smuzhiyun
max172xx_temperature_to_ps(unsigned int reg)98*4882a593Smuzhiyun static inline int max172xx_temperature_to_ps(unsigned int reg)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun int val = (int16_t)(reg);
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun return val * 10 / 256; /* in tenths of deg. C */
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun /*
106*4882a593Smuzhiyun * Calculating current registers resolution:
107*4882a593Smuzhiyun *
108*4882a593Smuzhiyun * RSense stored in 10^-5 Ohm, so mesaurment voltage must be
109*4882a593Smuzhiyun * in 10^-11 Volts for get current in uA.
110*4882a593Smuzhiyun * 16 bit current reg fullscale +/-51.2mV is 102400 uV.
111*4882a593Smuzhiyun * So: 102400 / 65535 * 10^5 = 156252
112*4882a593Smuzhiyun */
max172xx_current_to_voltage(unsigned int reg)113*4882a593Smuzhiyun static inline int max172xx_current_to_voltage(unsigned int reg)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun int val = (int16_t)(reg);
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun return val * 156252;
118*4882a593Smuzhiyun }
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun static inline struct max17211_device_info *
to_device_info(struct power_supply * psy)122*4882a593Smuzhiyun to_device_info(struct power_supply *psy)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun return power_supply_get_drvdata(psy);
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
max1721x_battery_get_property(struct power_supply * psy,enum power_supply_property psp,union power_supply_propval * val)127*4882a593Smuzhiyun static int max1721x_battery_get_property(struct power_supply *psy,
128*4882a593Smuzhiyun enum power_supply_property psp,
129*4882a593Smuzhiyun union power_supply_propval *val)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun struct max17211_device_info *info = to_device_info(psy);
132*4882a593Smuzhiyun unsigned int reg = 0;
133*4882a593Smuzhiyun int ret = 0;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun switch (psp) {
136*4882a593Smuzhiyun case POWER_SUPPLY_PROP_PRESENT:
137*4882a593Smuzhiyun /*
138*4882a593Smuzhiyun * POWER_SUPPLY_PROP_PRESENT will always readable via
139*4882a593Smuzhiyun * sysfs interface. Value return 0 if battery not
140*4882a593Smuzhiyun * present or unaccesable via W1.
141*4882a593Smuzhiyun */
142*4882a593Smuzhiyun val->intval =
143*4882a593Smuzhiyun regmap_read(info->regmap, MAX172XX_REG_STATUS,
144*4882a593Smuzhiyun ®) ? 0 : !(reg & MAX172XX_BAT_PRESENT);
145*4882a593Smuzhiyun break;
146*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CAPACITY:
147*4882a593Smuzhiyun ret = regmap_read(info->regmap, MAX172XX_REG_REPSOC, ®);
148*4882a593Smuzhiyun val->intval = max172xx_percent_to_ps(reg);
149*4882a593Smuzhiyun break;
150*4882a593Smuzhiyun case POWER_SUPPLY_PROP_VOLTAGE_NOW:
151*4882a593Smuzhiyun ret = regmap_read(info->regmap, MAX172XX_REG_BATT, ®);
152*4882a593Smuzhiyun val->intval = max172xx_voltage_to_ps(reg);
153*4882a593Smuzhiyun break;
154*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
155*4882a593Smuzhiyun ret = regmap_read(info->regmap, MAX172XX_REG_DESIGNCAP, ®);
156*4882a593Smuzhiyun val->intval = max172xx_capacity_to_ps(reg);
157*4882a593Smuzhiyun break;
158*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CHARGE_AVG:
159*4882a593Smuzhiyun ret = regmap_read(info->regmap, MAX172XX_REG_REPCAP, ®);
160*4882a593Smuzhiyun val->intval = max172xx_capacity_to_ps(reg);
161*4882a593Smuzhiyun break;
162*4882a593Smuzhiyun case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
163*4882a593Smuzhiyun ret = regmap_read(info->regmap, MAX172XX_REG_TTE, ®);
164*4882a593Smuzhiyun val->intval = max172xx_time_to_ps(reg);
165*4882a593Smuzhiyun break;
166*4882a593Smuzhiyun case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
167*4882a593Smuzhiyun ret = regmap_read(info->regmap, MAX172XX_REG_TTF, ®);
168*4882a593Smuzhiyun val->intval = max172xx_time_to_ps(reg);
169*4882a593Smuzhiyun break;
170*4882a593Smuzhiyun case POWER_SUPPLY_PROP_TEMP:
171*4882a593Smuzhiyun ret = regmap_read(info->regmap, MAX172XX_REG_TEMP, ®);
172*4882a593Smuzhiyun val->intval = max172xx_temperature_to_ps(reg);
173*4882a593Smuzhiyun break;
174*4882a593Smuzhiyun /* We need signed current, so must cast info->rsense to signed type */
175*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CURRENT_NOW:
176*4882a593Smuzhiyun ret = regmap_read(info->regmap, MAX172XX_REG_CURRENT, ®);
177*4882a593Smuzhiyun val->intval =
178*4882a593Smuzhiyun max172xx_current_to_voltage(reg) / (int)info->rsense;
179*4882a593Smuzhiyun break;
180*4882a593Smuzhiyun case POWER_SUPPLY_PROP_CURRENT_AVG:
181*4882a593Smuzhiyun ret = regmap_read(info->regmap, MAX172XX_REG_AVGCURRENT, ®);
182*4882a593Smuzhiyun val->intval =
183*4882a593Smuzhiyun max172xx_current_to_voltage(reg) / (int)info->rsense;
184*4882a593Smuzhiyun break;
185*4882a593Smuzhiyun /*
186*4882a593Smuzhiyun * Strings already received and inited by probe.
187*4882a593Smuzhiyun * We do dummy read for check battery still available.
188*4882a593Smuzhiyun */
189*4882a593Smuzhiyun case POWER_SUPPLY_PROP_MODEL_NAME:
190*4882a593Smuzhiyun ret = regmap_read(info->regmap, MAX1721X_REG_DEV_STR, ®);
191*4882a593Smuzhiyun val->strval = info->DeviceName;
192*4882a593Smuzhiyun break;
193*4882a593Smuzhiyun case POWER_SUPPLY_PROP_MANUFACTURER:
194*4882a593Smuzhiyun ret = regmap_read(info->regmap, MAX1721X_REG_MFG_STR, ®);
195*4882a593Smuzhiyun val->strval = info->ManufacturerName;
196*4882a593Smuzhiyun break;
197*4882a593Smuzhiyun case POWER_SUPPLY_PROP_SERIAL_NUMBER:
198*4882a593Smuzhiyun ret = regmap_read(info->regmap, MAX1721X_REG_SER_HEX, ®);
199*4882a593Smuzhiyun val->strval = info->SerialNumber;
200*4882a593Smuzhiyun break;
201*4882a593Smuzhiyun default:
202*4882a593Smuzhiyun ret = -EINVAL;
203*4882a593Smuzhiyun }
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun return ret;
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun static enum power_supply_property max1721x_battery_props[] = {
209*4882a593Smuzhiyun /* int */
210*4882a593Smuzhiyun POWER_SUPPLY_PROP_PRESENT,
211*4882a593Smuzhiyun POWER_SUPPLY_PROP_CAPACITY,
212*4882a593Smuzhiyun POWER_SUPPLY_PROP_VOLTAGE_NOW,
213*4882a593Smuzhiyun POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
214*4882a593Smuzhiyun POWER_SUPPLY_PROP_CHARGE_AVG,
215*4882a593Smuzhiyun POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
216*4882a593Smuzhiyun POWER_SUPPLY_PROP_TIME_TO_FULL_AVG,
217*4882a593Smuzhiyun POWER_SUPPLY_PROP_TEMP,
218*4882a593Smuzhiyun POWER_SUPPLY_PROP_CURRENT_NOW,
219*4882a593Smuzhiyun POWER_SUPPLY_PROP_CURRENT_AVG,
220*4882a593Smuzhiyun /* strings */
221*4882a593Smuzhiyun POWER_SUPPLY_PROP_MODEL_NAME,
222*4882a593Smuzhiyun POWER_SUPPLY_PROP_MANUFACTURER,
223*4882a593Smuzhiyun POWER_SUPPLY_PROP_SERIAL_NUMBER,
224*4882a593Smuzhiyun };
225*4882a593Smuzhiyun
get_string(struct max17211_device_info * info,uint16_t reg,uint8_t nr,char * str)226*4882a593Smuzhiyun static int get_string(struct max17211_device_info *info,
227*4882a593Smuzhiyun uint16_t reg, uint8_t nr, char *str)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun unsigned int val;
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun if (!str || !(reg == MAX1721X_REG_MFG_STR ||
232*4882a593Smuzhiyun reg == MAX1721X_REG_DEV_STR))
233*4882a593Smuzhiyun return -EFAULT;
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun while (nr--) {
236*4882a593Smuzhiyun if (regmap_read(info->regmap, reg++, &val))
237*4882a593Smuzhiyun return -EFAULT;
238*4882a593Smuzhiyun *str++ = val>>8 & 0x00FF;
239*4882a593Smuzhiyun *str++ = val & 0x00FF;
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun return 0;
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun /* Maxim say: Serial number is a hex string up to 12 hex characters */
get_sn_string(struct max17211_device_info * info,char * str)245*4882a593Smuzhiyun static int get_sn_string(struct max17211_device_info *info, char *str)
246*4882a593Smuzhiyun {
247*4882a593Smuzhiyun unsigned int val[3];
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun if (!str)
250*4882a593Smuzhiyun return -EFAULT;
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun if (regmap_read(info->regmap, MAX1721X_REG_SER_HEX, &val[0]))
253*4882a593Smuzhiyun return -EFAULT;
254*4882a593Smuzhiyun if (regmap_read(info->regmap, MAX1721X_REG_SER_HEX + 1, &val[1]))
255*4882a593Smuzhiyun return -EFAULT;
256*4882a593Smuzhiyun if (regmap_read(info->regmap, MAX1721X_REG_SER_HEX + 2, &val[2]))
257*4882a593Smuzhiyun return -EFAULT;
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun snprintf(str, 13, "%04X%04X%04X", val[0], val[1], val[2]);
260*4882a593Smuzhiyun return 0;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun /*
264*4882a593Smuzhiyun * MAX1721x registers description for w1-regmap
265*4882a593Smuzhiyun */
266*4882a593Smuzhiyun static const struct regmap_range max1721x_allow_range[] = {
267*4882a593Smuzhiyun regmap_reg_range(0, 0xDF), /* volatile data */
268*4882a593Smuzhiyun regmap_reg_range(0x180, 0x1DF), /* non-volatile memory */
269*4882a593Smuzhiyun regmap_reg_range(0x1E0, 0x1EF), /* non-volatile history (unused) */
270*4882a593Smuzhiyun };
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun static const struct regmap_range max1721x_deny_range[] = {
273*4882a593Smuzhiyun /* volatile data unused registers */
274*4882a593Smuzhiyun regmap_reg_range(0x24, 0x26),
275*4882a593Smuzhiyun regmap_reg_range(0x30, 0x31),
276*4882a593Smuzhiyun regmap_reg_range(0x33, 0x34),
277*4882a593Smuzhiyun regmap_reg_range(0x37, 0x37),
278*4882a593Smuzhiyun regmap_reg_range(0x3B, 0x3C),
279*4882a593Smuzhiyun regmap_reg_range(0x40, 0x41),
280*4882a593Smuzhiyun regmap_reg_range(0x43, 0x44),
281*4882a593Smuzhiyun regmap_reg_range(0x47, 0x49),
282*4882a593Smuzhiyun regmap_reg_range(0x4B, 0x4C),
283*4882a593Smuzhiyun regmap_reg_range(0x4E, 0xAF),
284*4882a593Smuzhiyun regmap_reg_range(0xB1, 0xB3),
285*4882a593Smuzhiyun regmap_reg_range(0xB5, 0xB7),
286*4882a593Smuzhiyun regmap_reg_range(0xBF, 0xD0),
287*4882a593Smuzhiyun regmap_reg_range(0xDB, 0xDB),
288*4882a593Smuzhiyun /* hole between volatile and non-volatile registers */
289*4882a593Smuzhiyun regmap_reg_range(0xE0, 0x17F),
290*4882a593Smuzhiyun };
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun static const struct regmap_access_table max1721x_regs = {
293*4882a593Smuzhiyun .yes_ranges = max1721x_allow_range,
294*4882a593Smuzhiyun .n_yes_ranges = ARRAY_SIZE(max1721x_allow_range),
295*4882a593Smuzhiyun .no_ranges = max1721x_deny_range,
296*4882a593Smuzhiyun .n_no_ranges = ARRAY_SIZE(max1721x_deny_range),
297*4882a593Smuzhiyun };
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun /*
300*4882a593Smuzhiyun * Model Gauge M5 Algorithm output register
301*4882a593Smuzhiyun * Volatile data (must not be cached)
302*4882a593Smuzhiyun */
303*4882a593Smuzhiyun static const struct regmap_range max1721x_volatile_allow[] = {
304*4882a593Smuzhiyun regmap_reg_range(0, 0xDF),
305*4882a593Smuzhiyun };
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun static const struct regmap_access_table max1721x_volatile_regs = {
308*4882a593Smuzhiyun .yes_ranges = max1721x_volatile_allow,
309*4882a593Smuzhiyun .n_yes_ranges = ARRAY_SIZE(max1721x_volatile_allow),
310*4882a593Smuzhiyun };
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun /*
313*4882a593Smuzhiyun * W1-regmap config
314*4882a593Smuzhiyun */
315*4882a593Smuzhiyun static const struct regmap_config max1721x_regmap_w1_config = {
316*4882a593Smuzhiyun .reg_bits = 16,
317*4882a593Smuzhiyun .val_bits = 16,
318*4882a593Smuzhiyun .rd_table = &max1721x_regs,
319*4882a593Smuzhiyun .volatile_table = &max1721x_volatile_regs,
320*4882a593Smuzhiyun .max_register = MAX1721X_MAX_REG_NR,
321*4882a593Smuzhiyun };
322*4882a593Smuzhiyun
devm_w1_max1721x_add_device(struct w1_slave * sl)323*4882a593Smuzhiyun static int devm_w1_max1721x_add_device(struct w1_slave *sl)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun struct power_supply_config psy_cfg = {};
326*4882a593Smuzhiyun struct max17211_device_info *info;
327*4882a593Smuzhiyun
328*4882a593Smuzhiyun info = devm_kzalloc(&sl->dev, sizeof(*info), GFP_KERNEL);
329*4882a593Smuzhiyun if (!info)
330*4882a593Smuzhiyun return -ENOMEM;
331*4882a593Smuzhiyun
332*4882a593Smuzhiyun sl->family_data = (void *)info;
333*4882a593Smuzhiyun info->w1_dev = &sl->dev;
334*4882a593Smuzhiyun
335*4882a593Smuzhiyun /*
336*4882a593Smuzhiyun * power_supply class battery name translated from W1 slave device
337*4882a593Smuzhiyun * unical ID (look like 26-0123456789AB) to "max1721x-0123456789AB\0"
338*4882a593Smuzhiyun * so, 26 (device family) correcpondent to max1721x devices.
339*4882a593Smuzhiyun * Device name still unical for any numbers connected devices.
340*4882a593Smuzhiyun */
341*4882a593Smuzhiyun snprintf(info->name, sizeof(info->name),
342*4882a593Smuzhiyun "max1721x-%012X", (unsigned int)sl->reg_num.id);
343*4882a593Smuzhiyun info->bat_desc.name = info->name;
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun /*
346*4882a593Smuzhiyun * FixMe: battery device name exceed max len for thermal_zone device
347*4882a593Smuzhiyun * name and translation to thermal_zone must be disabled.
348*4882a593Smuzhiyun */
349*4882a593Smuzhiyun info->bat_desc.no_thermal = true;
350*4882a593Smuzhiyun info->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
351*4882a593Smuzhiyun info->bat_desc.properties = max1721x_battery_props;
352*4882a593Smuzhiyun info->bat_desc.num_properties = ARRAY_SIZE(max1721x_battery_props);
353*4882a593Smuzhiyun info->bat_desc.get_property = max1721x_battery_get_property;
354*4882a593Smuzhiyun psy_cfg.drv_data = info;
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun /* regmap init */
357*4882a593Smuzhiyun info->regmap = devm_regmap_init_w1(info->w1_dev,
358*4882a593Smuzhiyun &max1721x_regmap_w1_config);
359*4882a593Smuzhiyun if (IS_ERR(info->regmap)) {
360*4882a593Smuzhiyun int err = PTR_ERR(info->regmap);
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun dev_err(info->w1_dev, "Failed to allocate register map: %d\n",
363*4882a593Smuzhiyun err);
364*4882a593Smuzhiyun return err;
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun /* rsense init */
368*4882a593Smuzhiyun info->rsense = 0;
369*4882a593Smuzhiyun if (regmap_read(info->regmap, MAX1721X_REG_NRSENSE, &info->rsense)) {
370*4882a593Smuzhiyun dev_err(info->w1_dev, "Can't read RSense. Hardware error.\n");
371*4882a593Smuzhiyun return -ENODEV;
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun if (!info->rsense) {
375*4882a593Smuzhiyun dev_warn(info->w1_dev, "RSense not calibrated, set 10 mOhms!\n");
376*4882a593Smuzhiyun info->rsense = 1000; /* in regs in 10^-5 */
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun dev_info(info->w1_dev, "RSense: %d mOhms.\n", info->rsense / 100);
379*4882a593Smuzhiyun
380*4882a593Smuzhiyun if (get_string(info, MAX1721X_REG_MFG_STR,
381*4882a593Smuzhiyun MAX1721X_REG_MFG_NUMB, info->ManufacturerName)) {
382*4882a593Smuzhiyun dev_err(info->w1_dev, "Can't read manufacturer. Hardware error.\n");
383*4882a593Smuzhiyun return -ENODEV;
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun if (!info->ManufacturerName[0])
387*4882a593Smuzhiyun strncpy(info->ManufacturerName, DEF_MFG_NAME,
388*4882a593Smuzhiyun 2 * MAX1721X_REG_MFG_NUMB);
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun if (get_string(info, MAX1721X_REG_DEV_STR,
391*4882a593Smuzhiyun MAX1721X_REG_DEV_NUMB, info->DeviceName)) {
392*4882a593Smuzhiyun dev_err(info->w1_dev, "Can't read device. Hardware error.\n");
393*4882a593Smuzhiyun return -ENODEV;
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun if (!info->DeviceName[0]) {
396*4882a593Smuzhiyun unsigned int dev_name;
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun if (regmap_read(info->regmap,
399*4882a593Smuzhiyun MAX172XX_REG_DEVNAME, &dev_name)) {
400*4882a593Smuzhiyun dev_err(info->w1_dev, "Can't read device name reg.\n");
401*4882a593Smuzhiyun return -ENODEV;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun
404*4882a593Smuzhiyun switch (dev_name & MAX172XX_DEV_MASK) {
405*4882a593Smuzhiyun case MAX172X1_DEV:
406*4882a593Smuzhiyun strncpy(info->DeviceName, DEF_DEV_NAME_MAX17211,
407*4882a593Smuzhiyun 2 * MAX1721X_REG_DEV_NUMB);
408*4882a593Smuzhiyun break;
409*4882a593Smuzhiyun case MAX172X5_DEV:
410*4882a593Smuzhiyun strncpy(info->DeviceName, DEF_DEV_NAME_MAX17215,
411*4882a593Smuzhiyun 2 * MAX1721X_REG_DEV_NUMB);
412*4882a593Smuzhiyun break;
413*4882a593Smuzhiyun default:
414*4882a593Smuzhiyun strncpy(info->DeviceName, DEF_DEV_NAME_UNKNOWN,
415*4882a593Smuzhiyun 2 * MAX1721X_REG_DEV_NUMB);
416*4882a593Smuzhiyun }
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun if (get_sn_string(info, info->SerialNumber)) {
420*4882a593Smuzhiyun dev_err(info->w1_dev, "Can't read serial. Hardware error.\n");
421*4882a593Smuzhiyun return -ENODEV;
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun
424*4882a593Smuzhiyun info->bat = devm_power_supply_register(&sl->dev, &info->bat_desc,
425*4882a593Smuzhiyun &psy_cfg);
426*4882a593Smuzhiyun if (IS_ERR(info->bat)) {
427*4882a593Smuzhiyun dev_err(info->w1_dev, "failed to register battery\n");
428*4882a593Smuzhiyun return PTR_ERR(info->bat);
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun return 0;
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun static const struct w1_family_ops w1_max1721x_fops = {
435*4882a593Smuzhiyun .add_slave = devm_w1_max1721x_add_device,
436*4882a593Smuzhiyun };
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun static struct w1_family w1_max1721x_family = {
439*4882a593Smuzhiyun .fid = W1_MAX1721X_FAMILY_ID,
440*4882a593Smuzhiyun .fops = &w1_max1721x_fops,
441*4882a593Smuzhiyun };
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun module_w1_family(w1_max1721x_family);
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun MODULE_LICENSE("GPL");
446*4882a593Smuzhiyun MODULE_AUTHOR("Alex A. Mihaylov <minimumlaw@rambler.ru>");
447*4882a593Smuzhiyun MODULE_DESCRIPTION("Maxim MAX17211/MAX17215 Fuel Gauage IC driver");
448*4882a593Smuzhiyun MODULE_ALIAS("w1-family-" __stringify(W1_MAX1721X_FAMILY_ID));
449