1 | // SPDX-License-Identifier: GPL-2.0-only |
2 | /* |
3 | * 1-wire client/driver for the Maxim/Dallas DS2781 Stand-Alone Fuel Gauge IC |
4 | * |
5 | * Author: Renata Sayakhova <renata@oktetlabs.ru> |
6 | * |
7 | * Based on ds2780_battery drivers |
8 | */ |
9 | |
10 | #include <linux/module.h> |
11 | #include <linux/slab.h> |
12 | #include <linux/param.h> |
13 | #include <linux/pm.h> |
14 | #include <linux/platform_device.h> |
15 | #include <linux/power_supply.h> |
16 | #include <linux/idr.h> |
17 | |
18 | #include <linux/w1.h> |
19 | #include "../../w1/slaves/w1_ds2781.h" |
20 | |
21 | /* Current unit measurement in uA for a 1 milli-ohm sense resistor */ |
22 | #define DS2781_CURRENT_UNITS 1563 |
23 | /* Charge unit measurement in uAh for a 1 milli-ohm sense resistor */ |
24 | #define DS2781_CHARGE_UNITS 6250 |
25 | /* Number of bytes in user EEPROM space */ |
26 | #define DS2781_USER_EEPROM_SIZE (DS2781_EEPROM_BLOCK0_END - \ |
27 | DS2781_EEPROM_BLOCK0_START + 1) |
28 | /* Number of bytes in parameter EEPROM space */ |
29 | #define DS2781_PARAM_EEPROM_SIZE (DS2781_EEPROM_BLOCK1_END - \ |
30 | DS2781_EEPROM_BLOCK1_START + 1) |
31 | |
32 | struct ds2781_device_info { |
33 | struct device *dev; |
34 | struct power_supply *bat; |
35 | struct power_supply_desc bat_desc; |
36 | struct device *w1_dev; |
37 | }; |
38 | |
39 | enum current_types { |
40 | CURRENT_NOW, |
41 | CURRENT_AVG, |
42 | }; |
43 | |
44 | static const char model[] = "DS2781" ; |
45 | static const char manufacturer[] = "Maxim/Dallas" ; |
46 | |
47 | static inline struct ds2781_device_info * |
48 | to_ds2781_device_info(struct power_supply *psy) |
49 | { |
50 | return power_supply_get_drvdata(psy); |
51 | } |
52 | |
53 | static inline int ds2781_battery_io(struct ds2781_device_info *dev_info, |
54 | char *buf, int addr, size_t count, int io) |
55 | { |
56 | return w1_ds2781_io(dev: dev_info->w1_dev, buf, addr, count, io); |
57 | } |
58 | |
59 | static int w1_ds2781_read(struct ds2781_device_info *dev_info, char *buf, |
60 | int addr, size_t count) |
61 | { |
62 | return ds2781_battery_io(dev_info, buf, addr, count, io: 0); |
63 | } |
64 | |
65 | static inline int ds2781_read8(struct ds2781_device_info *dev_info, u8 *val, |
66 | int addr) |
67 | { |
68 | return ds2781_battery_io(dev_info, buf: val, addr, count: sizeof(u8), io: 0); |
69 | } |
70 | |
71 | static int ds2781_read16(struct ds2781_device_info *dev_info, s16 *val, |
72 | int addr) |
73 | { |
74 | int ret; |
75 | u8 raw[2]; |
76 | |
77 | ret = ds2781_battery_io(dev_info, buf: raw, addr, count: sizeof(raw), io: 0); |
78 | if (ret < 0) |
79 | return ret; |
80 | |
81 | *val = (raw[0] << 8) | raw[1]; |
82 | |
83 | return 0; |
84 | } |
85 | |
86 | static inline int ds2781_read_block(struct ds2781_device_info *dev_info, |
87 | u8 *val, int addr, size_t count) |
88 | { |
89 | return ds2781_battery_io(dev_info, buf: val, addr, count, io: 0); |
90 | } |
91 | |
92 | static inline int ds2781_write(struct ds2781_device_info *dev_info, u8 *val, |
93 | int addr, size_t count) |
94 | { |
95 | return ds2781_battery_io(dev_info, buf: val, addr, count, io: 1); |
96 | } |
97 | |
98 | static inline int ds2781_store_eeprom(struct device *dev, int addr) |
99 | { |
100 | return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_COPY_DATA); |
101 | } |
102 | |
103 | static inline int ds2781_recall_eeprom(struct device *dev, int addr) |
104 | { |
105 | return w1_ds2781_eeprom_cmd(dev, addr, W1_DS2781_RECALL_DATA); |
106 | } |
107 | |
108 | static int ds2781_save_eeprom(struct ds2781_device_info *dev_info, int reg) |
109 | { |
110 | int ret; |
111 | |
112 | ret = ds2781_store_eeprom(dev: dev_info->w1_dev, addr: reg); |
113 | if (ret < 0) |
114 | return ret; |
115 | |
116 | ret = ds2781_recall_eeprom(dev: dev_info->w1_dev, addr: reg); |
117 | if (ret < 0) |
118 | return ret; |
119 | |
120 | return 0; |
121 | } |
122 | |
123 | /* Set sense resistor value in mhos */ |
124 | static int ds2781_set_sense_register(struct ds2781_device_info *dev_info, |
125 | u8 conductance) |
126 | { |
127 | int ret; |
128 | |
129 | ret = ds2781_write(dev_info, val: &conductance, |
130 | DS2781_RSNSP, count: sizeof(u8)); |
131 | if (ret < 0) |
132 | return ret; |
133 | |
134 | return ds2781_save_eeprom(dev_info, DS2781_RSNSP); |
135 | } |
136 | |
137 | /* Get RSGAIN value from 0 to 1.999 in steps of 0.001 */ |
138 | static int ds2781_get_rsgain_register(struct ds2781_device_info *dev_info, |
139 | u16 *rsgain) |
140 | { |
141 | return ds2781_read16(dev_info, val: rsgain, DS2781_RSGAIN_MSB); |
142 | } |
143 | |
144 | /* Set RSGAIN value from 0 to 1.999 in steps of 0.001 */ |
145 | static int ds2781_set_rsgain_register(struct ds2781_device_info *dev_info, |
146 | u16 rsgain) |
147 | { |
148 | int ret; |
149 | u8 raw[] = {rsgain >> 8, rsgain & 0xFF}; |
150 | |
151 | ret = ds2781_write(dev_info, val: raw, |
152 | DS2781_RSGAIN_MSB, count: sizeof(raw)); |
153 | if (ret < 0) |
154 | return ret; |
155 | |
156 | return ds2781_save_eeprom(dev_info, DS2781_RSGAIN_MSB); |
157 | } |
158 | |
159 | static int ds2781_get_voltage(struct ds2781_device_info *dev_info, |
160 | int *voltage_uV) |
161 | { |
162 | int ret; |
163 | char val[2]; |
164 | int voltage_raw; |
165 | |
166 | ret = w1_ds2781_read(dev_info, buf: val, DS2781_VOLT_MSB, count: 2 * sizeof(u8)); |
167 | if (ret < 0) |
168 | return ret; |
169 | /* |
170 | * The voltage value is located in 10 bits across the voltage MSB |
171 | * and LSB registers in two's complement form |
172 | * Sign bit of the voltage value is in bit 7 of the voltage MSB register |
173 | * Bits 9 - 3 of the voltage value are in bits 6 - 0 of the |
174 | * voltage MSB register |
175 | * Bits 2 - 0 of the voltage value are in bits 7 - 5 of the |
176 | * voltage LSB register |
177 | */ |
178 | voltage_raw = (val[0] << 3) | |
179 | (val[1] >> 5); |
180 | |
181 | /* DS2781 reports voltage in units of 9.76mV, but the battery class |
182 | * reports in units of uV, so convert by multiplying by 9760. */ |
183 | *voltage_uV = voltage_raw * 9760; |
184 | |
185 | return 0; |
186 | } |
187 | |
188 | static int ds2781_get_temperature(struct ds2781_device_info *dev_info, |
189 | int *temp) |
190 | { |
191 | int ret; |
192 | char val[2]; |
193 | int temp_raw; |
194 | |
195 | ret = w1_ds2781_read(dev_info, buf: val, DS2781_TEMP_MSB, count: 2 * sizeof(u8)); |
196 | if (ret < 0) |
197 | return ret; |
198 | /* |
199 | * The temperature value is located in 10 bits across the temperature |
200 | * MSB and LSB registers in two's complement form |
201 | * Sign bit of the temperature value is in bit 7 of the temperature |
202 | * MSB register |
203 | * Bits 9 - 3 of the temperature value are in bits 6 - 0 of the |
204 | * temperature MSB register |
205 | * Bits 2 - 0 of the temperature value are in bits 7 - 5 of the |
206 | * temperature LSB register |
207 | */ |
208 | temp_raw = ((val[0]) << 3) | |
209 | (val[1] >> 5); |
210 | *temp = temp_raw + (temp_raw / 4); |
211 | |
212 | return 0; |
213 | } |
214 | |
215 | static int ds2781_get_current(struct ds2781_device_info *dev_info, |
216 | enum current_types type, int *current_uA) |
217 | { |
218 | int ret, sense_res; |
219 | s16 current_raw; |
220 | u8 sense_res_raw, reg_msb; |
221 | |
222 | /* |
223 | * The units of measurement for current are dependent on the value of |
224 | * the sense resistor. |
225 | */ |
226 | ret = ds2781_read8(dev_info, val: &sense_res_raw, DS2781_RSNSP); |
227 | if (ret < 0) |
228 | return ret; |
229 | |
230 | if (sense_res_raw == 0) { |
231 | dev_err(dev_info->dev, "sense resistor value is 0\n" ); |
232 | return -EINVAL; |
233 | } |
234 | sense_res = 1000 / sense_res_raw; |
235 | |
236 | if (type == CURRENT_NOW) |
237 | reg_msb = DS2781_CURRENT_MSB; |
238 | else if (type == CURRENT_AVG) |
239 | reg_msb = DS2781_IAVG_MSB; |
240 | else |
241 | return -EINVAL; |
242 | |
243 | /* |
244 | * The current value is located in 16 bits across the current MSB |
245 | * and LSB registers in two's complement form |
246 | * Sign bit of the current value is in bit 7 of the current MSB register |
247 | * Bits 14 - 8 of the current value are in bits 6 - 0 of the current |
248 | * MSB register |
249 | * Bits 7 - 0 of the current value are in bits 7 - 0 of the current |
250 | * LSB register |
251 | */ |
252 | ret = ds2781_read16(dev_info, val: ¤t_raw, addr: reg_msb); |
253 | if (ret < 0) |
254 | return ret; |
255 | |
256 | *current_uA = current_raw * (DS2781_CURRENT_UNITS / sense_res); |
257 | return 0; |
258 | } |
259 | |
260 | static int ds2781_get_accumulated_current(struct ds2781_device_info *dev_info, |
261 | int *accumulated_current) |
262 | { |
263 | int ret, sense_res; |
264 | s16 current_raw; |
265 | u8 sense_res_raw; |
266 | |
267 | /* |
268 | * The units of measurement for accumulated current are dependent on |
269 | * the value of the sense resistor. |
270 | */ |
271 | ret = ds2781_read8(dev_info, val: &sense_res_raw, DS2781_RSNSP); |
272 | if (ret < 0) |
273 | return ret; |
274 | |
275 | if (sense_res_raw == 0) { |
276 | dev_err(dev_info->dev, "sense resistor value is 0\n" ); |
277 | return -EINVAL; |
278 | } |
279 | sense_res = 1000 / sense_res_raw; |
280 | |
281 | /* |
282 | * The ACR value is located in 16 bits across the ACR MSB and |
283 | * LSB registers |
284 | * Bits 15 - 8 of the ACR value are in bits 7 - 0 of the ACR |
285 | * MSB register |
286 | * Bits 7 - 0 of the ACR value are in bits 7 - 0 of the ACR |
287 | * LSB register |
288 | */ |
289 | ret = ds2781_read16(dev_info, val: ¤t_raw, DS2781_ACR_MSB); |
290 | if (ret < 0) |
291 | return ret; |
292 | |
293 | *accumulated_current = current_raw * (DS2781_CHARGE_UNITS / sense_res); |
294 | return 0; |
295 | } |
296 | |
297 | static int ds2781_get_capacity(struct ds2781_device_info *dev_info, |
298 | int *capacity) |
299 | { |
300 | int ret; |
301 | u8 raw; |
302 | |
303 | ret = ds2781_read8(dev_info, val: &raw, DS2781_RARC); |
304 | if (ret < 0) |
305 | return ret; |
306 | |
307 | *capacity = raw; |
308 | return 0; |
309 | } |
310 | |
311 | static int ds2781_get_status(struct ds2781_device_info *dev_info, int *status) |
312 | { |
313 | int ret, current_uA, capacity; |
314 | |
315 | ret = ds2781_get_current(dev_info, type: CURRENT_NOW, current_uA: ¤t_uA); |
316 | if (ret < 0) |
317 | return ret; |
318 | |
319 | ret = ds2781_get_capacity(dev_info, capacity: &capacity); |
320 | if (ret < 0) |
321 | return ret; |
322 | |
323 | if (power_supply_am_i_supplied(psy: dev_info->bat)) { |
324 | if (capacity == 100) |
325 | *status = POWER_SUPPLY_STATUS_FULL; |
326 | else if (current_uA > 50000) |
327 | *status = POWER_SUPPLY_STATUS_CHARGING; |
328 | else |
329 | *status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
330 | } else { |
331 | *status = POWER_SUPPLY_STATUS_DISCHARGING; |
332 | } |
333 | return 0; |
334 | } |
335 | |
336 | static int ds2781_get_charge_now(struct ds2781_device_info *dev_info, |
337 | int *charge_now) |
338 | { |
339 | int ret; |
340 | u16 charge_raw; |
341 | |
342 | /* |
343 | * The RAAC value is located in 16 bits across the RAAC MSB and |
344 | * LSB registers |
345 | * Bits 15 - 8 of the RAAC value are in bits 7 - 0 of the RAAC |
346 | * MSB register |
347 | * Bits 7 - 0 of the RAAC value are in bits 7 - 0 of the RAAC |
348 | * LSB register |
349 | */ |
350 | ret = ds2781_read16(dev_info, val: &charge_raw, DS2781_RAAC_MSB); |
351 | if (ret < 0) |
352 | return ret; |
353 | |
354 | *charge_now = charge_raw * 1600; |
355 | return 0; |
356 | } |
357 | |
358 | static int ds2781_get_control_register(struct ds2781_device_info *dev_info, |
359 | u8 *control_reg) |
360 | { |
361 | return ds2781_read8(dev_info, val: control_reg, DS2781_CONTROL); |
362 | } |
363 | |
364 | static int ds2781_set_control_register(struct ds2781_device_info *dev_info, |
365 | u8 control_reg) |
366 | { |
367 | int ret; |
368 | |
369 | ret = ds2781_write(dev_info, val: &control_reg, |
370 | DS2781_CONTROL, count: sizeof(u8)); |
371 | if (ret < 0) |
372 | return ret; |
373 | |
374 | return ds2781_save_eeprom(dev_info, DS2781_CONTROL); |
375 | } |
376 | |
377 | static int ds2781_battery_get_property(struct power_supply *psy, |
378 | enum power_supply_property psp, |
379 | union power_supply_propval *val) |
380 | { |
381 | int ret = 0; |
382 | struct ds2781_device_info *dev_info = to_ds2781_device_info(psy); |
383 | |
384 | switch (psp) { |
385 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
386 | ret = ds2781_get_voltage(dev_info, voltage_uV: &val->intval); |
387 | break; |
388 | |
389 | case POWER_SUPPLY_PROP_TEMP: |
390 | ret = ds2781_get_temperature(dev_info, temp: &val->intval); |
391 | break; |
392 | |
393 | case POWER_SUPPLY_PROP_MODEL_NAME: |
394 | val->strval = model; |
395 | break; |
396 | |
397 | case POWER_SUPPLY_PROP_MANUFACTURER: |
398 | val->strval = manufacturer; |
399 | break; |
400 | |
401 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
402 | ret = ds2781_get_current(dev_info, type: CURRENT_NOW, current_uA: &val->intval); |
403 | break; |
404 | |
405 | case POWER_SUPPLY_PROP_CURRENT_AVG: |
406 | ret = ds2781_get_current(dev_info, type: CURRENT_AVG, current_uA: &val->intval); |
407 | break; |
408 | |
409 | case POWER_SUPPLY_PROP_STATUS: |
410 | ret = ds2781_get_status(dev_info, status: &val->intval); |
411 | break; |
412 | |
413 | case POWER_SUPPLY_PROP_CAPACITY: |
414 | ret = ds2781_get_capacity(dev_info, capacity: &val->intval); |
415 | break; |
416 | |
417 | case POWER_SUPPLY_PROP_CHARGE_COUNTER: |
418 | ret = ds2781_get_accumulated_current(dev_info, accumulated_current: &val->intval); |
419 | break; |
420 | |
421 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
422 | ret = ds2781_get_charge_now(dev_info, charge_now: &val->intval); |
423 | break; |
424 | |
425 | default: |
426 | ret = -EINVAL; |
427 | } |
428 | |
429 | return ret; |
430 | } |
431 | |
432 | static enum power_supply_property ds2781_battery_props[] = { |
433 | POWER_SUPPLY_PROP_STATUS, |
434 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
435 | POWER_SUPPLY_PROP_TEMP, |
436 | POWER_SUPPLY_PROP_MODEL_NAME, |
437 | POWER_SUPPLY_PROP_MANUFACTURER, |
438 | POWER_SUPPLY_PROP_CURRENT_NOW, |
439 | POWER_SUPPLY_PROP_CURRENT_AVG, |
440 | POWER_SUPPLY_PROP_CAPACITY, |
441 | POWER_SUPPLY_PROP_CHARGE_COUNTER, |
442 | POWER_SUPPLY_PROP_CHARGE_NOW, |
443 | }; |
444 | |
445 | static ssize_t ds2781_get_pmod_enabled(struct device *dev, |
446 | struct device_attribute *attr, |
447 | char *buf) |
448 | { |
449 | int ret; |
450 | u8 control_reg; |
451 | struct power_supply *psy = to_power_supply(dev); |
452 | struct ds2781_device_info *dev_info = to_ds2781_device_info(psy); |
453 | |
454 | /* Get power mode */ |
455 | ret = ds2781_get_control_register(dev_info, control_reg: &control_reg); |
456 | if (ret < 0) |
457 | return ret; |
458 | |
459 | return sysfs_emit(buf, fmt: "%d\n" , |
460 | !!(control_reg & DS2781_CONTROL_PMOD)); |
461 | } |
462 | |
463 | static ssize_t ds2781_set_pmod_enabled(struct device *dev, |
464 | struct device_attribute *attr, |
465 | const char *buf, |
466 | size_t count) |
467 | { |
468 | int ret; |
469 | u8 control_reg, new_setting; |
470 | struct power_supply *psy = to_power_supply(dev); |
471 | struct ds2781_device_info *dev_info = to_ds2781_device_info(psy); |
472 | |
473 | /* Set power mode */ |
474 | ret = ds2781_get_control_register(dev_info, control_reg: &control_reg); |
475 | if (ret < 0) |
476 | return ret; |
477 | |
478 | ret = kstrtou8(s: buf, base: 0, res: &new_setting); |
479 | if (ret < 0) |
480 | return ret; |
481 | |
482 | if ((new_setting != 0) && (new_setting != 1)) { |
483 | dev_err(dev_info->dev, "Invalid pmod setting (0 or 1)\n" ); |
484 | return -EINVAL; |
485 | } |
486 | |
487 | if (new_setting) |
488 | control_reg |= DS2781_CONTROL_PMOD; |
489 | else |
490 | control_reg &= ~DS2781_CONTROL_PMOD; |
491 | |
492 | ret = ds2781_set_control_register(dev_info, control_reg); |
493 | if (ret < 0) |
494 | return ret; |
495 | |
496 | return count; |
497 | } |
498 | |
499 | static ssize_t ds2781_get_sense_resistor_value(struct device *dev, |
500 | struct device_attribute *attr, |
501 | char *buf) |
502 | { |
503 | int ret; |
504 | u8 sense_resistor; |
505 | struct power_supply *psy = to_power_supply(dev); |
506 | struct ds2781_device_info *dev_info = to_ds2781_device_info(psy); |
507 | |
508 | ret = ds2781_read8(dev_info, val: &sense_resistor, DS2781_RSNSP); |
509 | if (ret < 0) |
510 | return ret; |
511 | |
512 | ret = sysfs_emit(buf, fmt: "%d\n" , sense_resistor); |
513 | return ret; |
514 | } |
515 | |
516 | static ssize_t ds2781_set_sense_resistor_value(struct device *dev, |
517 | struct device_attribute *attr, |
518 | const char *buf, |
519 | size_t count) |
520 | { |
521 | int ret; |
522 | u8 new_setting; |
523 | struct power_supply *psy = to_power_supply(dev); |
524 | struct ds2781_device_info *dev_info = to_ds2781_device_info(psy); |
525 | |
526 | ret = kstrtou8(s: buf, base: 0, res: &new_setting); |
527 | if (ret < 0) |
528 | return ret; |
529 | |
530 | ret = ds2781_set_sense_register(dev_info, conductance: new_setting); |
531 | if (ret < 0) |
532 | return ret; |
533 | |
534 | return count; |
535 | } |
536 | |
537 | static ssize_t ds2781_get_rsgain_setting(struct device *dev, |
538 | struct device_attribute *attr, |
539 | char *buf) |
540 | { |
541 | int ret; |
542 | u16 rsgain; |
543 | struct power_supply *psy = to_power_supply(dev); |
544 | struct ds2781_device_info *dev_info = to_ds2781_device_info(psy); |
545 | |
546 | ret = ds2781_get_rsgain_register(dev_info, rsgain: &rsgain); |
547 | if (ret < 0) |
548 | return ret; |
549 | |
550 | return sysfs_emit(buf, fmt: "%d\n" , rsgain); |
551 | } |
552 | |
553 | static ssize_t ds2781_set_rsgain_setting(struct device *dev, |
554 | struct device_attribute *attr, |
555 | const char *buf, |
556 | size_t count) |
557 | { |
558 | int ret; |
559 | u16 new_setting; |
560 | struct power_supply *psy = to_power_supply(dev); |
561 | struct ds2781_device_info *dev_info = to_ds2781_device_info(psy); |
562 | |
563 | ret = kstrtou16(s: buf, base: 0, res: &new_setting); |
564 | if (ret < 0) |
565 | return ret; |
566 | |
567 | /* Gain can only be from 0 to 1.999 in steps of .001 */ |
568 | if (new_setting > 1999) { |
569 | dev_err(dev_info->dev, "Invalid rsgain setting (0 - 1999)\n" ); |
570 | return -EINVAL; |
571 | } |
572 | |
573 | ret = ds2781_set_rsgain_register(dev_info, rsgain: new_setting); |
574 | if (ret < 0) |
575 | return ret; |
576 | |
577 | return count; |
578 | } |
579 | |
580 | static ssize_t ds2781_get_pio_pin(struct device *dev, |
581 | struct device_attribute *attr, |
582 | char *buf) |
583 | { |
584 | int ret; |
585 | u8 sfr; |
586 | struct power_supply *psy = to_power_supply(dev); |
587 | struct ds2781_device_info *dev_info = to_ds2781_device_info(psy); |
588 | |
589 | ret = ds2781_read8(dev_info, val: &sfr, DS2781_SFR); |
590 | if (ret < 0) |
591 | return ret; |
592 | |
593 | ret = sysfs_emit(buf, fmt: "%d\n" , sfr & DS2781_SFR_PIOSC); |
594 | return ret; |
595 | } |
596 | |
597 | static ssize_t ds2781_set_pio_pin(struct device *dev, |
598 | struct device_attribute *attr, |
599 | const char *buf, |
600 | size_t count) |
601 | { |
602 | int ret; |
603 | u8 new_setting; |
604 | struct power_supply *psy = to_power_supply(dev); |
605 | struct ds2781_device_info *dev_info = to_ds2781_device_info(psy); |
606 | |
607 | ret = kstrtou8(s: buf, base: 0, res: &new_setting); |
608 | if (ret < 0) |
609 | return ret; |
610 | |
611 | if ((new_setting != 0) && (new_setting != 1)) { |
612 | dev_err(dev_info->dev, "Invalid pio_pin setting (0 or 1)\n" ); |
613 | return -EINVAL; |
614 | } |
615 | |
616 | ret = ds2781_write(dev_info, val: &new_setting, |
617 | DS2781_SFR, count: sizeof(u8)); |
618 | if (ret < 0) |
619 | return ret; |
620 | |
621 | return count; |
622 | } |
623 | |
624 | static ssize_t ds2781_read_param_eeprom_bin(struct file *filp, |
625 | struct kobject *kobj, |
626 | struct bin_attribute *bin_attr, |
627 | char *buf, loff_t off, size_t count) |
628 | { |
629 | struct device *dev = kobj_to_dev(kobj); |
630 | struct power_supply *psy = to_power_supply(dev); |
631 | struct ds2781_device_info *dev_info = to_ds2781_device_info(psy); |
632 | |
633 | return ds2781_read_block(dev_info, val: buf, |
634 | DS2781_EEPROM_BLOCK1_START + off, count); |
635 | } |
636 | |
637 | static ssize_t ds2781_write_param_eeprom_bin(struct file *filp, |
638 | struct kobject *kobj, |
639 | struct bin_attribute *bin_attr, |
640 | char *buf, loff_t off, size_t count) |
641 | { |
642 | struct device *dev = kobj_to_dev(kobj); |
643 | struct power_supply *psy = to_power_supply(dev); |
644 | struct ds2781_device_info *dev_info = to_ds2781_device_info(psy); |
645 | int ret; |
646 | |
647 | ret = ds2781_write(dev_info, val: buf, |
648 | DS2781_EEPROM_BLOCK1_START + off, count); |
649 | if (ret < 0) |
650 | return ret; |
651 | |
652 | ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK1_START); |
653 | if (ret < 0) |
654 | return ret; |
655 | |
656 | return count; |
657 | } |
658 | |
659 | static struct bin_attribute ds2781_param_eeprom_bin_attr = { |
660 | .attr = { |
661 | .name = "param_eeprom" , |
662 | .mode = S_IRUGO | S_IWUSR, |
663 | }, |
664 | .size = DS2781_PARAM_EEPROM_SIZE, |
665 | .read = ds2781_read_param_eeprom_bin, |
666 | .write = ds2781_write_param_eeprom_bin, |
667 | }; |
668 | |
669 | static ssize_t ds2781_read_user_eeprom_bin(struct file *filp, |
670 | struct kobject *kobj, |
671 | struct bin_attribute *bin_attr, |
672 | char *buf, loff_t off, size_t count) |
673 | { |
674 | struct device *dev = kobj_to_dev(kobj); |
675 | struct power_supply *psy = to_power_supply(dev); |
676 | struct ds2781_device_info *dev_info = to_ds2781_device_info(psy); |
677 | |
678 | return ds2781_read_block(dev_info, val: buf, |
679 | DS2781_EEPROM_BLOCK0_START + off, count); |
680 | |
681 | } |
682 | |
683 | static ssize_t ds2781_write_user_eeprom_bin(struct file *filp, |
684 | struct kobject *kobj, |
685 | struct bin_attribute *bin_attr, |
686 | char *buf, loff_t off, size_t count) |
687 | { |
688 | struct device *dev = kobj_to_dev(kobj); |
689 | struct power_supply *psy = to_power_supply(dev); |
690 | struct ds2781_device_info *dev_info = to_ds2781_device_info(psy); |
691 | int ret; |
692 | |
693 | ret = ds2781_write(dev_info, val: buf, |
694 | DS2781_EEPROM_BLOCK0_START + off, count); |
695 | if (ret < 0) |
696 | return ret; |
697 | |
698 | ret = ds2781_save_eeprom(dev_info, DS2781_EEPROM_BLOCK0_START); |
699 | if (ret < 0) |
700 | return ret; |
701 | |
702 | return count; |
703 | } |
704 | |
705 | static struct bin_attribute ds2781_user_eeprom_bin_attr = { |
706 | .attr = { |
707 | .name = "user_eeprom" , |
708 | .mode = S_IRUGO | S_IWUSR, |
709 | }, |
710 | .size = DS2781_USER_EEPROM_SIZE, |
711 | .read = ds2781_read_user_eeprom_bin, |
712 | .write = ds2781_write_user_eeprom_bin, |
713 | }; |
714 | |
715 | static DEVICE_ATTR(pmod_enabled, S_IRUGO | S_IWUSR, ds2781_get_pmod_enabled, |
716 | ds2781_set_pmod_enabled); |
717 | static DEVICE_ATTR(sense_resistor_value, S_IRUGO | S_IWUSR, |
718 | ds2781_get_sense_resistor_value, ds2781_set_sense_resistor_value); |
719 | static DEVICE_ATTR(rsgain_setting, S_IRUGO | S_IWUSR, ds2781_get_rsgain_setting, |
720 | ds2781_set_rsgain_setting); |
721 | static DEVICE_ATTR(pio_pin, S_IRUGO | S_IWUSR, ds2781_get_pio_pin, |
722 | ds2781_set_pio_pin); |
723 | |
724 | static struct attribute *ds2781_sysfs_attrs[] = { |
725 | &dev_attr_pmod_enabled.attr, |
726 | &dev_attr_sense_resistor_value.attr, |
727 | &dev_attr_rsgain_setting.attr, |
728 | &dev_attr_pio_pin.attr, |
729 | NULL |
730 | }; |
731 | |
732 | static struct bin_attribute *ds2781_sysfs_bin_attrs[] = { |
733 | &ds2781_param_eeprom_bin_attr, |
734 | &ds2781_user_eeprom_bin_attr, |
735 | NULL, |
736 | }; |
737 | |
738 | static const struct attribute_group ds2781_sysfs_group = { |
739 | .attrs = ds2781_sysfs_attrs, |
740 | .bin_attrs = ds2781_sysfs_bin_attrs, |
741 | |
742 | }; |
743 | |
744 | static const struct attribute_group *ds2781_sysfs_groups[] = { |
745 | &ds2781_sysfs_group, |
746 | NULL, |
747 | }; |
748 | |
749 | static int ds2781_battery_probe(struct platform_device *pdev) |
750 | { |
751 | struct power_supply_config psy_cfg = {}; |
752 | struct ds2781_device_info *dev_info; |
753 | |
754 | dev_info = devm_kzalloc(dev: &pdev->dev, size: sizeof(*dev_info), GFP_KERNEL); |
755 | if (!dev_info) |
756 | return -ENOMEM; |
757 | |
758 | platform_set_drvdata(pdev, data: dev_info); |
759 | |
760 | dev_info->dev = &pdev->dev; |
761 | dev_info->w1_dev = pdev->dev.parent; |
762 | dev_info->bat_desc.name = dev_name(dev: &pdev->dev); |
763 | dev_info->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY; |
764 | dev_info->bat_desc.properties = ds2781_battery_props; |
765 | dev_info->bat_desc.num_properties = ARRAY_SIZE(ds2781_battery_props); |
766 | dev_info->bat_desc.get_property = ds2781_battery_get_property; |
767 | |
768 | psy_cfg.drv_data = dev_info; |
769 | psy_cfg.attr_grp = ds2781_sysfs_groups; |
770 | |
771 | dev_info->bat = devm_power_supply_register(parent: &pdev->dev, |
772 | desc: &dev_info->bat_desc, |
773 | cfg: &psy_cfg); |
774 | if (IS_ERR(ptr: dev_info->bat)) { |
775 | dev_err(dev_info->dev, "failed to register battery\n" ); |
776 | return PTR_ERR(ptr: dev_info->bat); |
777 | } |
778 | |
779 | return 0; |
780 | } |
781 | |
782 | static struct platform_driver ds2781_battery_driver = { |
783 | .driver = { |
784 | .name = "ds2781-battery" , |
785 | }, |
786 | .probe = ds2781_battery_probe, |
787 | }; |
788 | module_platform_driver(ds2781_battery_driver); |
789 | |
790 | MODULE_LICENSE("GPL" ); |
791 | MODULE_AUTHOR("Renata Sayakhova <renata@oktetlabs.ru>" ); |
792 | MODULE_DESCRIPTION("Maxim/Dallas DS2781 Stand-Alone Fuel Gauge IC driver" ); |
793 | MODULE_ALIAS("platform:ds2781-battery" ); |
794 | |
795 | |