Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1633301 > unrolled thread

[PATCH 1/2] Add support for OneWire (W1) devices family 0x26 (MAX17211/MAX17215)

Started by"Alex A. Mihaylov" <minimumlaw@rambler.ru>
First post2017-04-29 16:40 +0200
Last post2017-05-01 08:20 +0200
Articles 5 — 2 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH 1/2] Add support for OneWire (W1) devices family 0x26 (MAX17211/MAX17215) "Alex A. Mihaylov" <minimumlaw@rambler.ru> - 2017-04-29 16:40 +0200
    Re: [PATCH 1/2] Add support for OneWire (W1) devices family 0x26  (MAX17211/MAX17215) Sebastian Reichel <sre@kernel.org> - 2017-04-29 18:00 +0200
      Re: [PATCH 1/2] Add support for OneWire (W1) devices family 0x26  (MAX17211/MAX17215) "Alex A. Mihaylov" <minimumlaw@rambler.ru> - 2017-04-30 07:30 +0200
        Re: [PATCH 1/2] Add support for OneWire (W1) devices family 0x26  (MAX17211/MAX17215) Sebastian Reichel <sre@kernel.org> - 2017-04-30 23:00 +0200
          Re: [PATCH 1/2] Add support for OneWire (W1) devices family 0x26  (MAX17211/MAX17215) "Alex A. Mihaylov" <minimumlaw@rambler.ru> - 2017-05-01 08:20 +0200

#1633301 — [PATCH 1/2] Add support for OneWire (W1) devices family 0x26 (MAX17211/MAX17215)

From"Alex A. Mihaylov" <minimumlaw@rambler.ru>
Date2017-04-29 16:40 +0200
Subject[PATCH 1/2] Add support for OneWire (W1) devices family 0x26 (MAX17211/MAX17215)
Message-ID<tBBHr-7p2-5@gated-at.bofh.it>
Maxim Semiconductor MAX17211/MAX17215 single/multi-cell fuel gauge
monitor with M5 Fuel Gauge algorithm

Slave device provide software layer for access to internal registers
MAX17211/MAX17215 chip.
---
 drivers/w1/slaves/Kconfig       |  12 ++++
 drivers/w1/slaves/Makefile      |   1 +
 drivers/w1/slaves/w1_max1721x.c | 121 ++++++++++++++++++++++++++++++++++++++++
 drivers/w1/slaves/w1_max1721x.h | 101 +++++++++++++++++++++++++++++++++
 drivers/w1/w1_family.h          |   1 +
 5 files changed, 236 insertions(+)
 create mode 100644 drivers/w1/slaves/w1_max1721x.c
 create mode 100644 drivers/w1/slaves/w1_max1721x.h

diff --git a/drivers/w1/slaves/Kconfig b/drivers/w1/slaves/Kconfig
index cfe74d0..05467da 100644
--- a/drivers/w1/slaves/Kconfig
+++ b/drivers/w1/slaves/Kconfig
@@ -78,6 +78,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
diff --git a/drivers/w1/slaves/Makefile b/drivers/w1/slaves/Makefile
index 1e9989a..1e9f942 100644
--- a/drivers/w1/slaves/Makefile
+++ b/drivers/w1/slaves/Makefile
@@ -10,6 +10,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
diff --git a/drivers/w1/slaves/w1_max1721x.c b/drivers/w1/slaves/w1_max1721x.c
new file mode 100644
index 0000000..bb73e90
--- /dev/null
+++ b/drivers/w1/slaves/w1_max1721x.c
@@ -0,0 +1,121 @@
+/*
+ * 1-Wire implementation for the max17211 chip
+ *
+ * Copyright © 2017, Alex A. Mihaylov <minimumlaw@rambler.ru>
+ *
+ * Use consistent with the GNU GPL is permitted,
+ * provided that this copyright notice is
+ * preserved in its entirety in all copies and derived works.
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/types.h>
+#include <linux/platform_device.h>
+#include <linux/mutex.h>
+#include <linux/idr.h>
+#include <linux/gfp.h>
+
+#include "../w1.h"
+#include "../w1_int.h"
+#include "../w1_family.h"
+#include "w1_max1721x.h"
+
+int w1_max1721x_reg_get(struct device *dev, uint16_t addr, uint16_t *val)
+{
+	struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
+	int ret = -ENODEV;
+
+	if (addr > MAX1721X_MAX_REG_NR || addr < 0 || !val || !dev)
+		return -EFAULT;
+
+	mutex_lock(&sl->master->bus_mutex);
+
+	if (!w1_reset_select_slave(sl)) {
+		w1_write_8(sl->master, W1_MAX1721X_READ_DATA);
+		w1_write_8(sl->master, addr & 0x00FF);
+		w1_write_8(sl->master, addr>>8 & 0x00FF);
+		*val = w1_read_8(sl->master);
+		*val |= w1_read_8(sl->master)<<8;
+		ret = 0;
+	}
+
+	mutex_unlock(&sl->master->bus_mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL(w1_max1721x_reg_get);
+
+int w1_max1721x_reg_set(struct device *dev, uint16_t addr, uint16_t val)
+{
+	struct w1_slave *sl = container_of(dev, struct w1_slave, dev);
+	int ret = -ENODEV;
+
+	if (addr > MAX1721X_MAX_REG_NR || addr < 0 || !dev)
+		return -EFAULT;
+
+	mutex_lock(&sl->master->bus_mutex);
+
+	if (!w1_reset_select_slave(sl)) {
+		w1_write_8(sl->master, W1_MAX1721X_READ_DATA);
+		w1_write_8(sl->master, addr & 0x00FF);
+		w1_write_8(sl->master, addr>>8 & 0x00FF);
+		w1_write_8(sl->master, val & 0x00FF);
+		w1_write_8(sl->master, val>>8 & 0x00FF);
+		ret = 0;
+	}
+
+	mutex_unlock(&sl->master->bus_mutex);
+
+	return ret;
+}
+EXPORT_SYMBOL(w1_max1721x_reg_set);
+
+static int w1_max17211_add_device(struct w1_slave *sl)
+{
+	int ret;
+	struct platform_device *pdev;
+
+	pdev = platform_device_alloc("max1721x-battery", PLATFORM_DEVID_AUTO);
+	if (!pdev)
+		return -ENOMEM;
+	pdev->dev.parent = &sl->dev;
+
+	ret = platform_device_add(pdev);
+	if (ret)
+		goto pdev_add_failed;
+
+	dev_set_drvdata(&sl->dev, pdev);
+
+	return 0;
+
+pdev_add_failed:
+	platform_device_put(pdev);
+
+	return ret;
+}
+
+static void w1_max17211_remove_device(struct w1_slave *sl)
+{
+	struct platform_device *pdev = dev_get_drvdata(&sl->dev);
+
+	platform_device_unregister(pdev);
+}
+
+static struct w1_family_ops w1_max17211_fops = {
+	.add_slave    = w1_max17211_add_device,
+	.remove_slave = w1_max17211_remove_device,
+};
+
+static struct w1_family w1_max17211_family = {
+	.fid = W1_FAMILY_MAX17211,
+	.fops = &w1_max17211_fops,
+};
+module_w1_family(w1_max17211_family);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Alex A. Mihaylov <minimumlaw@rambler.ru>");
+MODULE_DESCRIPTION("1-wire Driver for MAX17211/MAX17215 battery monitor");
+MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_MAX17211));
diff --git a/drivers/w1/slaves/w1_max1721x.h b/drivers/w1/slaves/w1_max1721x.h
new file mode 100644
index 0000000..1deb3d3
--- /dev/null
+++ b/drivers/w1/slaves/w1_max1721x.h
@@ -0,0 +1,101 @@
+/*
+ * 1-Wire implementation for the max17211 chip
+ *
+ * Copyright © 2017, Alex A. Mihaylov <minimumlaw@rambler.ru>
+ *
+ * Use consistent with the GNU GPL is permitted,
+ * provided that this copyright notice is
+ * preserved in its entirety in all copies and derived works.
+ *
+ */
+
+#ifndef __w1_max17211_h__
+#define __w1_max17211_h__
+
+/* Known commands to the MAX1721X chip */
+#define W1_MAX1721X_READ_DATA		0x69
+#define W1_MAX1721X_WRITE_DATA		0x6C
+
+/* Factory settings (nonvilatile registers) (W1 specific) */
+
+#define MAX1721X_REG_NRSENSE	0x1CF	/* RSense in 10^-5 Ohm */
+/* Strings */
+#define MAX1721X_REG_MFG_STR	0x1CC
+#define MAX1721X_REG_MFG_NUMB	3
+#define MAX1721X_REG_DEV_STR	0x1DB
+#define MAX1721X_REG_DEV_NUMB	5
+/* HEX Strings */
+#define MAX1721X_REG_SER_HEX	0x1D8
+
+/* Number of valid register addresses */
+#define MAX1721X_MAX_REG_NR	(0x1EF)
+
+/* MAX1721X/MAX17215 Output Registers for I2C and W1 chips */
+
+#define MAX172XX_REG_STATUS	0x000	/* status reg */
+#define MAX172XX_BAT_PRESENT	(1<<4)	/* battery connected bit */
+#define MAX172XX_REG_DEVNAME	0x021	/* chip config */
+#define MAX172XX_DEV_MASK	0x000F	/* chip type mask */
+#define MAX172X1_DEV		0x0001
+#define MAX172X5_DEV		0x0005
+#define MAX172XX_REG_TEMP	0x008	/* Temperature */
+#define MAX172XX_REG_BATT	0x0DA	/* Battery voltage */
+#define MAX172XX_REG_CURRENT	0x00A	/* Actual current */
+#define MAX172XX_REG_AVGCURRENT	0x00B	/* Average current */
+#define MAX172XX_REG_REPSOC	0x006	/* Percentage of charge */
+#define MAX172XX_REG_DESIGNCAP	0x018	/* Design capacity */
+#define MAX172XX_REG_REPCAP	0x005	/* Average capacity */
+#define MAX172XX_REG_TTE	0x011	/* Time to empty */
+#define MAX172XX_REG_TTF	0x020	/* Time to full */
+
+/* Convert regs value to power_supply units */
+
+static inline int max172xx_time_to_ps(uint16_t reg)
+{
+	return reg * 5625 / 1000;	/* in sec. */
+}
+
+static inline int max172xx_percent_to_ps(uint16_t reg)
+{
+	return reg / 256;	/* in percent from 0 to 100 */
+}
+
+static inline int max172xx_voltage_to_ps(uint16_t reg)
+{
+	return reg * 1250;	/* in uV */
+}
+
+static inline int max172xx_capacity_to_ps(uint16_t reg)
+{
+	return reg * 500;	/* in uAh */
+}
+
+/*
+ * Current and temperature is signed values, so unsigned regs
+ * value must be converted to signed type
+ */
+
+static inline int max172xx_temperature_to_ps(uint16_t reg)
+{
+	return (int16_t)reg * 10 / 256;	/* in tenths of deg. C */
+}
+
+/*
+ * Calculating current registers resolution:
+ *
+ * RSense stored in 10^-5 Ohm, so mesaurment voltage must be
+ * in 10^-11 Volts for get current in uA.
+ * 16 bit current reg fullscale +/-51.2mV is 102400 uV.
+ * So: 102400 / 65535 * 10^5 = 156252
+ */
+static inline int max172xx_current_to_voltage(uint16_t reg)
+{
+	return (int16_t)reg * 156252;
+}
+
+extern int w1_max1721x_reg_get(struct device *dev, uint16_t addr,
+			uint16_t *val);
+extern int w1_max1721x_reg_set(struct device *dev, uint16_t addr,
+			uint16_t val);
+
+#endif /* !__w1_max17211_h__ */
diff --git a/drivers/w1/w1_family.h b/drivers/w1/w1_family.h
index 10a7a07..797c162 100644
--- a/drivers/w1/w1_family.h
+++ b/drivers/w1/w1_family.h
@@ -35,6 +35,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
-- 
2.8.4 (Apple Git-73)

[toc] | [next] | [standalone]


#1633305 — Re: [PATCH 1/2] Add support for OneWire (W1) devices family 0x26 (MAX17211/MAX17215)

FromSebastian Reichel <sre@kernel.org>
Date2017-04-29 18:00 +0200
SubjectRe: [PATCH 1/2] Add support for OneWire (W1) devices family 0x26 (MAX17211/MAX17215)
Message-ID<tBCWR-86K-3@gated-at.bofh.it>
In reply to#1633301

[Multipart message — attachments visible in raw view] — view raw

Hi,

On Sat, Apr 29, 2017 at 05:34:28PM +0300, Alex A. Mihaylov wrote:
> Maxim Semiconductor MAX17211/MAX17215 single/multi-cell fuel gauge
> monitor with M5 Fuel Gauge algorithm
> 
> Slave device provide software layer for access to internal registers
> MAX17211/MAX17215 chip.

Please convert this to regmap. There should be lots of docs and
examples around, for example:

http://elinux.org/images/a/a3/Regmap-_The_Power_of_Subsystems_and_Abstractions.pdf

There is no generic w1 handler, but you can provide custom
read/write functions. Also it would be nice to have this
based on https://lkml.org/lkml/2017/3/16/604. Then everything
could go into the power-supply driver.

-- Sebastian

[toc] | [prev] | [next] | [standalone]


#1633389 — Re: [PATCH 1/2] Add support for OneWire (W1) devices family 0x26 (MAX17211/MAX17215)

From"Alex A. Mihaylov" <minimumlaw@rambler.ru>
Date2017-04-30 07:30 +0200
SubjectRe: [PATCH 1/2] Add support for OneWire (W1) devices family 0x26 (MAX17211/MAX17215)
Message-ID<tBPAK-7C7-27@gated-at.bofh.it>
In reply to#1633305
Hi,


>> Slave device provide software layer for access to internal registers
>> MAX17211/MAX17215 chip.
> Please convert this to regmap.There is no generic w1 handler, but you can provide custom
> read/write functions.
I think regmap be overkill for this driver. Here we need access to a 
small number of registers.  Registers are extremely simple on the 
internal device. As a result, the software layer of regmap will only 
complicate the readability and understanding of the code. And also 
increase the size and time of code execution, and even complicate the 
perception.

> Also it would be nice to have this
> based on https://lkml.org/lkml/2017/3/16/604. Then everything
> could go into the power-supply driver.
>
No problems. Once this set of patches will be accepted in the mainline.

Alex.

[toc] | [prev] | [next] | [standalone]


#1633493 — Re: [PATCH 1/2] Add support for OneWire (W1) devices family 0x26 (MAX17211/MAX17215)

FromSebastian Reichel <sre@kernel.org>
Date2017-04-30 23:00 +0200
SubjectRe: [PATCH 1/2] Add support for OneWire (W1) devices family 0x26 (MAX17211/MAX17215)
Message-ID<tC46J-7Zu-11@gated-at.bofh.it>
In reply to#1633389

[Multipart message — attachments visible in raw view] — view raw

Hi,

On Sun, Apr 30, 2017 at 08:21:51AM +0300, Alex A. Mihaylov wrote:
> > > Slave device provide software layer for access to internal registers
> > > MAX17211/MAX17215 chip.
> > Please convert this to regmap.There is no generic w1 handler, but you can provide custom
> > read/write functions.
> I think regmap be overkill for this driver. Here we need access to a small
> number of registers.  Registers are extremely simple on the internal device.
> As a result, the software layer of regmap will only complicate the
> readability and understanding of the code. And also increase the size and
> time of code execution, and even complicate the perception.

I did not ask for full usage of all regmap features. Just register
the read/write handler as regmap handler and use regmap_read/write
to get values.

-- Sebastian

[toc] | [prev] | [next] | [standalone]


#1633560 — Re: [PATCH 1/2] Add support for OneWire (W1) devices family 0x26 (MAX17211/MAX17215)

From"Alex A. Mihaylov" <minimumlaw@rambler.ru>
Date2017-05-01 08:20 +0200
SubjectRe: [PATCH 1/2] Add support for OneWire (W1) devices family 0x26 (MAX17211/MAX17215)
Message-ID<tCcQF-5tc-9@gated-at.bofh.it>
In reply to#1633493
Hi!


30.04.17 23:53, Sebastian Reichel wrote::
>>>> Slave device provide software layer for access to internal registers
>>>> MAX17211/MAX17215 chip.
>>> Please convert this to regmap.There is no generic w1 handler, but you can provide custom
>>> read/write functions.
>> I think regmap be overkill for this driver. the perception.
> I did not ask for full usage of all regmap features. Just register
> the read/write handler as regmap handler and use regmap_read/write
> to get values.
>
OK, I think about this. But max17211-battery use ONLY 
w1_max12711x_reg_get(). This is very simple function.

Second function w1_max1721x_reg_set() writen for extend feature list 
(factory calibrating and init battery).  I think, this code must _NOT_ 
be writen, and must _NOT_ be accessible for end user. Change calibration 
values potentially can damage battery or device. Theory, I can unexport 
this function and remove them from drivers code.

So output registers of Maxim M5 Fuel Gauge algorithm very simple: one 
register - one value. In driver I have two points with bitfield. First - 
battery connected from power supply props. Second: very optional device 
type from probe. This value _NOT_ used from driver - just write device 
type, if manufacturer not fill this data in chip nvram.

This moment driver use 14 registers. Ok, let's some time above will be 
14*3=42 registers accessible with w1_max1721x_reg_get(). This value is 
greatly overestimated - so many properties are not in the class 
POWER_SUPPLY. But register range in MAX1721X is 0x1F0 (496 words or 992 
bytes) of RAM for cache registers. As embedded system developer I 
dislike so ram management.

As result I repeat: regmap will be overkill for this driver. But OK, I 
try to write this code. Just for fun.

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web