Skip to contentSkip to conversation

Battery monitor MAX1721x with w1-regmap and w1-slave

PATCH In review Alex A. Mihaylov 3 participants 8 messages May 28, 2017 – May 30, 2017

Subsystems (4)
lore
Share & tools
lore
#1 lore raw

PATCH 1/3 regmap: Add OneWire (W1) bus support

In reply to Alex A. Mihaylov

Add basic support regmap (register map access) API for OneWire (W1) bus

---
 drivers/base/regmap/Kconfig     |   6 +-
 drivers/base/regmap/Makefile    |   1 +
 drivers/base/regmap/regmap-w1.c | 241 ++++++++++++++++++++++++++++++++++++++++
 include/linux/regmap.h          |  34 ++++++
 4 files changed, 281 insertions(+), 1 deletion(-)
 create mode 100644 drivers/base/regmap/regmap-w1.c
drivers/base/regmap/Kconfig +5 -1
index db9d00c..413af5f 100644
@@ -3,7 +3,7 @@
 # subsystems should select the appropriate symbols.
 
 config REGMAP
-	default y if (REGMAP_I2C || REGMAP_SPI || REGMAP_SPMI || REGMAP_AC97 || REGMAP_MMIO || REGMAP_IRQ)
+	default y if (REGMAP_I2C || REGMAP_SPI || REGMAP_SPMI || REGMAP_W1 || REGMAP_AC97 || REGMAP_MMIO || REGMAP_IRQ)
 	select LZO_COMPRESS
 	select LZO_DECOMPRESS
 	select IRQ_DOMAIN if REGMAP_IRQ
@@ -24,6 +24,10 @@ config REGMAP_SPMI
 	tristate
 	depends on SPMI
 
+config REGMAP_W1
+	tristate
+	depends on W1
+
 config REGMAP_MMIO
 	tristate
 
drivers/base/regmap/Makefile +1 -0
index 609e4c8..17741ae 100644
@@ -10,3 +10,4 @@ obj-$(CONFIG_REGMAP_SPI) += regmap-spi.o
 obj-$(CONFIG_REGMAP_SPMI) += regmap-spmi.o
 obj-$(CONFIG_REGMAP_MMIO) += regmap-mmio.o
 obj-$(CONFIG_REGMAP_IRQ) += regmap-irq.o
+obj-$(CONFIG_REGMAP_W1) += regmap-w1.o
drivers/base/regmap/regmap-w1.c new file +241 -0

241 lines not on this page · raw message

include/linux/regmap.h +34 -0
index e886492..86eeacc 100644
@@ -461,6 +461,10 @@ struct regmap *__regmap_init_spmi_ext(struct spmi_device *dev,
 				      const struct regmap_config *config,
 				      struct lock_class_key *lock_key,
 				      const char *lock_name);
+struct regmap *__regmap_init_w1(struct device *w1_dev,
+				 const struct regmap_config *config,
+				 struct lock_class_key *lock_key,
+				 const char *lock_name);
 struct regmap *__regmap_init_mmio_clk(struct device *dev, const char *clk_id,
 				      void __iomem *regs,
 				      const struct regmap_config *config,
@@ -493,6 +497,10 @@ struct regmap *__devm_regmap_init_spmi_ext(struct spmi_device *dev,
 					   const struct regmap_config *config,
 					   struct lock_class_key *lock_key,
 					   const char *lock_name);
+struct regmap *__devm_regmap_init_w1(struct device *w1_dev,
+				      const struct regmap_config *config,
+				      struct lock_class_key *lock_key,
+				      const char *lock_name);
 struct regmap *__devm_regmap_init_mmio_clk(struct device *dev,
 					   const char *clk_id,
 					   void __iomem *regs,
@@ -597,6 +605,19 @@ int regmap_attach_dev(struct device *dev, struct regmap *map,
 				dev, config)
 
 /**
+ * regmap_init_w1() - Initialise register map
+ *
+ * @w1_dev: Device that will be interacted with
+ * @config: Configuration for register map
+ *
+ * The return value will be an ERR_PTR() on error or a valid pointer to
+ * a struct regmap.
+ */
+#define regmap_init_w1(w1_dev, config)					\
+	__regmap_lockdep_wrapper(__regmap_init_w1, #config,		\
+				w1_dev, config)
+
+/**
  * regmap_init_mmio_clk() - Initialise register map with register clock
  *
  * @dev: Device that will be interacted with
@@ -712,6 +733,19 @@ bool regmap_ac97_default_volatile(struct device *dev, unsigned int reg);
 				dev, config)
 
 /**
+ * devm_regmap_init_w1() - Initialise managed register map
+ *
+ * @w1_dev: Device that will be interacted with
+ * @config: Configuration for register map
+ *
+ * The return value will be an ERR_PTR() on error or a valid pointer
+ * to a struct regmap.  The regmap will be automatically freed by the
+ * device management code.
+ */
+#define devm_regmap_init_w1(w1_dev, config)				\
+	__regmap_lockdep_wrapper(__devm_regmap_init_w1, #config,	\
+				w1_dev, config)
+/**
  * devm_regmap_init_mmio_clk() - Initialise managed register map with clock
  *
  * @dev: Device that will be interacted with
Signature · 1 line
2.8.4 (Apple Git-73)
#6 lore raw

PATCH 2/3 w1: Add Maxim Semiconductor MAX1721x W1 slave drivers

In reply to Alex A. Mihaylov

Add support Maxim Semiconductor MAX17211/MAX17215 OneWire family 0x26

---
 drivers/w1/slaves/Kconfig       |  12 +++++
 drivers/w1/slaves/Makefile      |   1 +
 drivers/w1/slaves/w1_max1721x.c |  73 ++++++++++++++++++++++++++++
 drivers/w1/slaves/w1_max1721x.h | 102 ++++++++++++++++++++++++++++++++++++++++
 drivers/w1/w1_family.h          |   1 +
 5 files changed, 189 insertions(+)
 create mode 100644 drivers/w1/slaves/w1_max1721x.c
 create mode 100644 drivers/w1/slaves/w1_max1721x.h
drivers/w1/slaves/Kconfig +12 -0
index 0ef9f26..06a63a1 100644
@@ -86,6 +86,18 @@ config W1_SLAVE_DS2433_CRC
 	  Each block has 30 bytes of data and a two byte CRC16.
 	  Full block writes are only allowed if the CRC is valid.
 
+config W1_SLAVE_MAX1721X
+	tristate "Maxim MAX17211/MAX17215 battery monitor chip"
+	help
+	  If you enable this you will have the MAX17211/MAX17215 battery
+	  monitor chip support.
+
+	  The battery monitor chip is used in many batteries/devices
+	  as the one who is responsible for charging/discharging/monitoring
+	  Li+ batteries.
+
+	  If you are unsure, say N.
+
 config W1_SLAVE_DS2760
 	tristate "Dallas 2760 battery monitor chip (HP iPAQ & others)"
 	help
drivers/w1/slaves/Makefile +1 -0
index b4a3589..967329a 100644
@@ -11,6 +11,7 @@ obj-$(CONFIG_W1_SLAVE_DS2406)	+= w1_ds2406.o
 obj-$(CONFIG_W1_SLAVE_DS2423)	+= w1_ds2423.o
 obj-$(CONFIG_W1_SLAVE_DS2431)	+= w1_ds2431.o
 obj-$(CONFIG_W1_SLAVE_DS2433)	+= w1_ds2433.o
+obj-$(CONFIG_W1_SLAVE_MAX1721X)	+= w1_max1721x.o
 obj-$(CONFIG_W1_SLAVE_DS2760)	+= w1_ds2760.o
 obj-$(CONFIG_W1_SLAVE_DS2780)	+= w1_ds2780.o
 obj-$(CONFIG_W1_SLAVE_DS2781)	+= w1_ds2781.o
drivers/w1/slaves/w1_max1721x.c new file +73 -0

73 lines not on this page · raw message

drivers/w1/slaves/w1_max1721x.h new file +102 -0

102 lines not on this page · raw message

drivers/w1/w1_family.h +1 -0
index c4a6b25..398adc9 100644
@@ -29,6 +29,7 @@
 #define W1_COUNTER_DS2423	0x1D
 #define W1_THERM_DS1822  	0x22
 #define W1_EEPROM_DS2433  	0x23
+#define W1_FAMILY_MAX17211	0x26
 #define W1_THERM_DS18B20 	0x28
 #define W1_FAMILY_DS2408	0x29
 #define W1_EEPROM_DS2431	0x2D
Signature · 1 line
2.8.4 (Apple Git-73)
#7 lore raw

PATCH 3/3 power: supply: Add support MAX1721x battery monitor

In reply to Alex A. Mihaylov

Add support for battery monitor MAX1721x chips (power_supply class). Maxim Semiconductor MAX1721x Standalone Fuel Gauge battery monitor. MAX17211 used for singlecell batteryes, MAX17215 for multicell.

---
 drivers/power/supply/Kconfig            |  14 ++
 drivers/power/supply/Makefile           |   1 +
 drivers/power/supply/max1721x_battery.c | 357 ++++++++++++++++++++++++++++++++
 3 files changed, 372 insertions(+)
 create mode 100644 drivers/power/supply/max1721x_battery.c
drivers/power/supply/Kconfig +14 -0
index da54ac8..0df90ac 100644
@@ -268,6 +268,20 @@ config BATTERY_MAX17042
 	  with MAX17042. This driver also supports max17047/50 chips which are
 	  improved version of max17042.
 
+config BATTERY_MAX1721X
+	tristate "MAX17211/MAX17215 standalone gas-gauge"
+	depends on W1
+	select W1_SLAVE_MAX1721X
+	select REGMAP_W1
+	help
+	  MAX1721x is fuel-gauge systems for lithium-ion (Li+) batteries
+	  in handheld and portable equipment. MAX17211 used with single cell
+	  battery. MAX17215 designed for muticell battery. Both them have
+	  OneWire (W1) host interface.
+
+	  Say Y here to enable support for the MAX17211/MAX17215 standalone
+	  battery gas-gauge.
+
 config BATTERY_Z2
 	tristate "Z2 battery driver"
 	depends on I2C && MACH_ZIPIT2
drivers/power/supply/Makefile +1 -0
index 3789a2c..c785d05 100644
@@ -41,6 +41,7 @@ obj-$(CONFIG_CHARGER_DA9150)	+= da9150-charger.o
 obj-$(CONFIG_BATTERY_DA9150)	+= da9150-fg.o
 obj-$(CONFIG_BATTERY_MAX17040)	+= max17040_battery.o
 obj-$(CONFIG_BATTERY_MAX17042)	+= max17042_battery.o
+obj-$(CONFIG_BATTERY_MAX1721X)	+= max1721x_battery.o
 obj-$(CONFIG_BATTERY_Z2)	+= z2_battery.o
 obj-$(CONFIG_BATTERY_RT5033)	+= rt5033_battery.o
 obj-$(CONFIG_CHARGER_RT9455)	+= rt9455_charger.o
drivers/power/supply/max1721x_battery.c new file +357 -0

357 lines not on this page · raw message

Signature · 1 line
2.8.4 (Apple Git-73)
#0 lore raw

cover Battery monitor MAX1721x with w1-regmap and w1-slave

regmap: Add OneWire (W1) bus support
w1: Add Maxim Semiconductor MAX1721X W1 slave drivers
power: supply: Add support MAX1721x battery monitor

 drivers/base/regmap/Kconfig             |   6 +-
 drivers/base/regmap/Makefile            |   1 +
 drivers/base/regmap/regmap-w1.c         | 241 +++++++++++++++++++++
 drivers/power/supply/Kconfig            |  14 ++
 drivers/power/supply/Makefile           |   1 +
 drivers/power/supply/max1721x_battery.c | 357 ++++++++++++++++++++++++++++++++
 drivers/w1/slaves/Kconfig               |  12 ++
 drivers/w1/slaves/Makefile              |   1 +
 drivers/w1/slaves/w1_max1721x.c         |  73 +++++++
 drivers/w1/slaves/w1_max1721x.h         | 102 +++++++++
 drivers/w1/w1_family.h                  |   1 +
 include/linux/regmap.h                  |  34 +++
 12 files changed, 842 insertions(+), 1 deletion(-)
 create mode 100644 drivers/base/regmap/regmap-w1.c
 create mode 100644 drivers/power/supply/max1721x_battery.c
 create mode 100644 drivers/w1/slaves/w1_max1721x.c
 create mode 100644 drivers/w1/slaves/w1_max1721x.h
Signature · 1 line
2.8.4 (Apple Git-73)
maintainer
#2 lore raw

Re: [PATCH 1/3] regmap: Add OneWire (W1) bus support

In reply to Alex A. Mihaylov

drivers/base/regmap/regmap-w1.c line 41

Quoted 9 lines · Alex A. Mihaylov
drivers/base/regmap/regmap-w1.c new file +9 -0
+	int ret = -ENODEV;
+
+	mutex_lock(&sl->master->bus_mutex);
+	if (!w1_reset_select_slave(sl)) {
+		w1_write_8(sl->master, W1_CMD_READ_DATA);
+		w1_write_8(sl->master, reg);
+		*val = w1_read_8(sl->master);
+		ret = 0;
+	}

I asked you to move the error handling into the else case in these :(

#5 lore raw

Re: [PATCH 1/3] regmap: Add OneWire (W1) bus support

In reply to Alex A. Mihaylov
Quoted 1 line · Alex A. Mihaylov

Add basic support regmap (register map access) API for OneWire (W1) bus

It's called "1-wire" or "1-Wire" almost everywhere else in the kernel, and on the Maxim
website (except in the URL
https://www.maximintegrated.com/en/products/digital/one-wire.html ;-)

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#3 lore raw

Re: [PATCH 1/3] regmap: Add OneWire (W1) bus support

In reply to Mark Brown

29.05.17 16:13, Mark Brown пишет:

Quoted 10 lines

drivers/base/regmap/regmap-w1.c line 41

Quoted 9 lines
drivers/base/regmap/regmap-w1.c new file +9 -0
+	int ret = -ENODEV;
+
+	mutex_lock(&sl->master->bus_mutex);
+	if (!w1_reset_select_slave(sl)) {
+		w1_write_8(sl->master, W1_CMD_READ_DATA);
+		w1_write_8(sl->master, reg);
+		*val = w1_read_8(sl->master);
+		ret = 0;
+	}

I asked you to move the error handling into the else case in these :(

Why do you want to see exactly the construction of if/else?

I already wrote that for me this will worsen the quality and understanding of the code. And another point - in fact it is not an error handler. This is a completely normal and permissible situation. There is no "disconnect" event for this bus, and consequently at any arbitrary time the kernel can not be sure that the device is still available.

My ret = -ENODEV is exactly about this.

I can see this pattern in other kernel place, like:
drivers/usb/musb/da8xx.c line 374 drivers/usb/musb/davinci.c line 381 drivers/usb/host/xhci-mtk.c line 531
and many other palces.

maintainer
#4 lore raw

Re: [PATCH 1/3] regmap: Add OneWire (W1) bus support

Quoted 5 lines · Alex A. Mihaylov

29.05.17 16:13, Mark Brown пишет:

Quoted 1 line

I asked you to move the error handling into the else case in these :(

Why do you want to see exactly the construction of if/else?

I want to see some error handling in the lock so it doesn't look like it's missing, especially since this is the unusual pattern of "mark it as OK in an if statement then carry on running". The code pattern is too unusual and there's too much code and blank space between the ret = 0 meaning it takes too much effort to slow down and check that there's not a bug.

Quoted 3 lines

I already wrote that for me this will worsen the quality and understanding of the code. And another point - in fact it is not an error handler. This is a completely normal and permissible situation. There is no "disconnect"

It's an error. It may be an expected and recoverable error but as far as this write is concerned it's an error.

Quoted 2 lines

I can see this pattern in other kernel place, like:
drivers/usb/musb/da8xx.c line 374

This one for example looks like normal code - it's jumping to error in the error handling cases, the success case is a normal direct return with the error cases in the if checks and there's multiple conditions that could trigger an error. You need to get out of the locked region so you have limited options but only have one conditional, moving the assignment inside the else seems like the clearest.