Skip to contentSkip to conversation

mfd: mt6360: add pmic mt6360 driver

PATCH Superseded Gene Chen 4 participants 6 messages Oct 22, 2019 – Oct 24, 2019

Subsystems (2)

v4 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Gene Chen

author
#0 lore raw

PATCH v4 1/1 mfd: mt6360: add pmic mt6360 driver

From: Gene Chen <[email protected]>

Add mfd driver for mt6360 pmic chip include
Battery Charger/USB_PD/Flash LED/RGB LED/LDO/Buck

---
 drivers/mfd/Kconfig                |  12 +
 drivers/mfd/Makefile               |   1 +
 drivers/mfd/mt6360-core.c          | 457 +++++++++++++++++++++++++++++++++++++
 include/linux/mfd/mt6360-private.h | 279 ++++++++++++++++++++++
 include/linux/mfd/mt6360.h         |  32 +++
 5 files changed, 781 insertions(+)
 create mode 100644 drivers/mfd/mt6360-core.c
 create mode 100644 include/linux/mfd/mt6360-private.h
 create mode 100644 include/linux/mfd/mt6360.h

changelogs between v1 & v2
- include missing header file

changelogs between v2 & v3
- add changelogs

changelogs between v3 & v4
- fix Kconfig description
- replace mt6360_pmu_info with mt6360_pmu_data
- replace probe with probe_new
- remove unnecessary irq_chip variable
- remove annotation
- replace MT6360_MFD_CELL with OF_MFD_CELL

drivers/mfd/Kconfig +12 -0
index ae24d3e..129c7585 100644
@@ -856,6 +856,18 @@ config MFD_MAX8998
 	  additional drivers must be enabled in order to use the functionality
 	  of the device.
 
+config MFD_MT6360
+	tristate "Mediatek MT6360 SubPMIC"
+	select MFD_CORE
+	select REGMAP_I2C
+	select REGMAP_IRQ
+	depends on I2C
+	help
+	  Say Y here to enable MT6360 PMU/PMIC/LDO functional support.
+	  PMU part includes Charger, Flashlight, RGB LED
+	  PMIC part includes 2-channel BUCKs and 2-channel LDOs
+	  LDO part includes 4-channel LDOs
+
 config MFD_MT6397
 	tristate "MediaTek MT6397 PMIC Support"
 	select MFD_CORE
drivers/mfd/Makefile +1 -0
index c1067ea..ba13785 100644
@@ -238,6 +238,7 @@ obj-$(CONFIG_INTEL_SOC_PMIC)	+= intel-soc-pmic.o
 obj-$(CONFIG_INTEL_SOC_PMIC_BXTWC)	+= intel_soc_pmic_bxtwc.o
 obj-$(CONFIG_INTEL_SOC_PMIC_CHTWC)	+= intel_soc_pmic_chtwc.o
 obj-$(CONFIG_INTEL_SOC_PMIC_CHTDC_TI)	+= intel_soc_pmic_chtdc_ti.o
+obj-$(CONFIG_MFD_MT6360)	+= mt6360-core.o
 mt6397-objs	:= mt6397-core.o mt6397-irq.o
 obj-$(CONFIG_MFD_MT6397)	+= mt6397.o
 obj-$(CONFIG_INTEL_SOC_PMIC_MRFLD)	+= intel_soc_pmic_mrfld.o
drivers/mfd/mt6360-core.c new file +457 -0

457 lines not on this page · raw message

include/linux/mfd/mt6360-private.h new file +279 -0

279 lines not on this page · raw message

include/linux/mfd/mt6360.h new file +32 -0
new file mode 100644
index 0000000..f9a189f
@@ -0,0 +1,32 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2019 MediaTek Inc.
+ */
+
+#ifndef __MT6360_H__
+#define __MT6360_H__
+
+#include <linux/regmap.h>
+
+enum {
+	MT6360_SLAVE_PMU = 0,
+	MT6360_SLAVE_PMIC,
+	MT6360_SLAVE_LDO,
+	MT6360_SLAVE_TCPC,
+	MT6360_SLAVE_MAX,
+};
+
+#define MT6360_PMU_SLAVEID	(0x34)
+#define MT6360_PMIC_SLAVEID	(0x1A)
+#define MT6360_LDO_SLAVEID	(0x64)
+#define MT6360_TCPC_SLAVEID	(0x4E)
+
+struct mt6360_pmu_data {
+	struct i2c_client *i2c[MT6360_SLAVE_MAX];
+	struct device *dev;
+	struct regmap *regmap;
+	struct regmap_irq_chip_data *irq_data;
+	unsigned int chip_rev;
+};
+
+#endif /* __MT6360_H__ */
Signature · 1 line
1.9.1
#1 lore raw

Wolfram,

Would you be kind enough to see below please?

I'd like to know if it looks sane to you.

Quoted 30 lines · Gene Chen

From: Gene Chen <[email protected]>

Add mfd driver for mt6360 pmic chip include
Battery Charger/USB_PD/Flash LED/RGB LED/LDO/Buck

---
 drivers/mfd/Kconfig                |  12 +
 drivers/mfd/Makefile               |   1 +
 drivers/mfd/mt6360-core.c          | 457 +++++++++++++++++++++++++++++++++++++
 include/linux/mfd/mt6360-private.h | 279 ++++++++++++++++++++++
 include/linux/mfd/mt6360.h         |  32 +++
 5 files changed, 781 insertions(+)
 create mode 100644 drivers/mfd/mt6360-core.c
 create mode 100644 include/linux/mfd/mt6360-private.h
 create mode 100644 include/linux/mfd/mt6360.h

changelogs between v1 & v2
- include missing header file

changelogs between v2 & v3
- add changelogs

changelogs between v3 & v4
- fix Kconfig description
- replace mt6360_pmu_info with mt6360_pmu_data
- replace probe with probe_new
- remove unnecessary irq_chip variable
- remove annotation
- replace MT6360_MFD_CELL with OF_MFD_CELL

[...]

Quoted 47 lines
drivers/mfd/mt6360-core.c new file +47 -0
+	for (i = 0; i < MT6360_SLAVE_MAX; i++) {
+		if (mt6360_slave_addr[i] == client->addr) {
+			mpd->i2c[i] = client;
+			continue;
+		}
+		mpd->i2c[i] = i2c_new_dummy(client->adapter,
+					    mt6360_slave_addr[i]);
+		if (!mpd->i2c[i]) {
+			dev_err(&client->dev, "new i2c dev [%d] fail\n", i);
+			ret = -ENODEV;
+			goto out;
+		}
+		i2c_set_clientdata(mpd->i2c[i], mpd);
+	}
+
+	ret = devm_mfd_add_devices(&client->dev, PLATFORM_DEVID_AUTO,
+				   mt6360_devs, ARRAY_SIZE(mt6360_devs), NULL,
+				   0, regmap_irq_get_domain(mpd->irq_data));
+	if (ret < 0) {
+		dev_err(&client->dev, "mfd add cells fail\n");
+		goto out;
+	}
+
+	return 0;
+out:
+	while (--i >= 0) {
+		if (mpd->i2c[i]->addr == client->addr)
+			continue;
+		i2c_unregister_device(mpd->i2c[i]);
+	}
+
+	return ret;
+}
+
+static int mt6360_pmu_remove(struct i2c_client *client)
+{
+	struct mt6360_pmu_data *mpd = i2c_get_clientdata(client);
+	int i;
+
+	for (i = 0; i < MT6360_SLAVE_MAX; i++) {
+		if (mpd->i2c[i]->addr == client->addr)
+			continue;
+		i2c_unregister_device(mpd->i2c[i]);
+	}
+
+	return 0;
+}
Signature · 4 lines
Lee Jones [李琼斯]
Linaro Services Technical Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#2 lore raw
Quoted 6 lines

drivers/mfd/mt6360-core.c line 363

Quoted 6 lines
drivers/mfd/mt6360-core.c new file +5 -0
+	for (i = 0; i < MT6360_SLAVE_MAX; i++) {
+		if (mt6360_slave_addr[i] == client->addr) {
+			mpd->i2c[i] = client;
+			continue;
+		}

Not knowing the DT bindings, I wonder if we can let the for-loop start at 1 and do beforehand:

	mpd->i2c[0] = client;

That would save the above if block. However, this is a minor nit.

Quoted 2 lines

drivers/mfd/mt6360-core.c line 365

Quoted 2 lines
drivers/mfd/mt6360-core.c new file +2 -0
+		mpd->i2c[i] = i2c_new_dummy(client->adapter,
+					    mt6360_slave_addr[i]);

Please use the new API i2c_new_dummy_device here...

Quoted 1 line

drivers/mfd/mt6360-core.c line 366

Quoted 1 line
drivers/mfd/mt6360-core.c new file +1 -0
+		if (!mpd->i2c[i]) {

... and IS_ERR() here.

Quoted 2 lines

drivers/mfd/mt6360-core.c line 368

Quoted 2 lines
drivers/mfd/mt6360-core.c new file +2 -0
+			dev_err(&client->dev, "new i2c dev [%d] fail\n", i);
+			ret = -ENODEV;

Then you can also return a proper errno value.

You can probably also use devm_i2c_new_dummy_device()...

Quoted 11 lines
Quoted 11 lines
drivers/mfd/mt6360-core.c new file +11 -0
+			goto out;
+		}
+		i2c_set_clientdata(mpd->i2c[i], mpd);
+	}
+
+	ret = devm_mfd_add_devices(&client->dev, PLATFORM_DEVID_AUTO,
+				   mt6360_devs, ARRAY_SIZE(mt6360_devs), NULL,
+				   0, regmap_irq_get_domain(mpd->irq_data));
+	if (ret < 0) {
+		dev_err(&client->dev, "mfd add cells fail\n");
+		goto out;
Quoted 8 lines

drivers/mfd/mt6360-core.c line 387

Quoted 8 lines
drivers/mfd/mt6360-core.c new file +8 -0
+	}
+
+	return 0;
+out:
+	while (--i >= 0) {
+		if (mpd->i2c[i]->addr == client->addr)
+			continue;
+		i2c_unregister_device(mpd->i2c[i]);

... to save this ...

Quoted 7 lines
Quoted 7 lines
drivers/mfd/mt6360-core.c new file +7 -0
+	}
+
+	return ret;
+}
+
+static int mt6360_pmu_remove(struct i2c_client *client)
+{
Quoted 8 lines

drivers/mfd/mt6360-core.c line 402

Quoted 8 lines
drivers/mfd/mt6360-core.c new file +8 -0
+	struct mt6360_pmu_data *mpd = i2c_get_clientdata(client);
+	int i;
+
+	for (i = 0; i < MT6360_SLAVE_MAX; i++) {
+		if (mpd->i2c[i]->addr == client->addr)
+			continue;
+		i2c_unregister_device(mpd->i2c[i]);
+	}

... and this. But please double check.

#3 lore raw
In reply to Gene Chen
Quoted 30 lines · Gene Chen

From: Gene Chen <[email protected]>

Add mfd driver for mt6360 pmic chip include
Battery Charger/USB_PD/Flash LED/RGB LED/LDO/Buck

---
 drivers/mfd/Kconfig                |  12 +
 drivers/mfd/Makefile               |   1 +
 drivers/mfd/mt6360-core.c          | 457 +++++++++++++++++++++++++++++++++++++
 include/linux/mfd/mt6360-private.h | 279 ++++++++++++++++++++++
 include/linux/mfd/mt6360.h         |  32 +++
 5 files changed, 781 insertions(+)
 create mode 100644 drivers/mfd/mt6360-core.c
 create mode 100644 include/linux/mfd/mt6360-private.h
 create mode 100644 include/linux/mfd/mt6360.h

changelogs between v1 & v2
- include missing header file

changelogs between v2 & v3
- add changelogs

changelogs between v3 & v4
- fix Kconfig description
- replace mt6360_pmu_info with mt6360_pmu_data
- replace probe with probe_new
- remove unnecessary irq_chip variable
- remove annotation
- replace MT6360_MFD_CELL with OF_MFD_CELL

[...]

Quoted 48 lines
include/linux/mfd/mt6360-private.h new file +48 -0
+/* IRQ definitions */
+struct mt6360_pmu_irq_desc {
+	const char *name;
+	irq_handler_t irq_handler;
+};
+
+#define  MT6360_DT_VALPROP(name, type) \
+			{#name, offsetof(type, name)}
+
+struct mt6360_val_prop {
+	const char *name;
+	size_t offset;
+};
+
+static inline void mt6360_dt_parser_helper(struct device_node *np, void *data,
+					   const struct mt6360_val_prop *props,
+					   int prop_cnt)
+{
+	int i;
+
+	for (i = 0; i < prop_cnt; i++) {
+		if (unlikely(!props[i].name))
+			continue;
+		of_property_read_u32(np, props[i].name, data + props[i].offset);
+	}
+}
+
+#define MT6360_PDATA_VALPROP(name, type, reg, shift, mask, func, base) \
+			{offsetof(type, name), reg, shift, mask, func, base}
+
+struct mt6360_pdata_prop {
+	size_t offset;
+	u8 reg;
+	u8 shift;
+	u8 mask;
+	u32 (*transform)(u32 val);
+	u8 base;
+};
+
+static inline int mt6360_pdata_apply_helper(void *context, void *pdata,
+					   const struct mt6360_pdata_prop *prop,
+					   int prop_cnt)
+{
+	int i, ret;
+	u32 val;
+
+	for (i = 0; i < prop_cnt; i++) {
+		val = *(u32 *)(pdata + prop[i].offset);

include/linux/mfd/mt6360-private.h line 277

Quoted 10 lines
include/linux/mfd/mt6360-private.h new file +10 -0
+		if (prop[i].transform)
+			val = prop[i].transform(val);
+		val += prop[i].base;
+		ret = regmap_update_bits(context,
+			     prop[i].reg, prop[i].mask, val << prop[i].shift);
+		if (ret < 0)
+			return ret;
+	}
+	return 0;
+}

Where are these helpers used?

Are they generic?

Why are they helpful to you, but not helpful to anyone else?

Signature · 4 lines
Lee Jones [李琼斯]
Linaro Services Technical Lead
Linaro.org │ Open source software for ARM SoCs
Follow Linaro: Facebook | Twitter | Blog
#4 lore raw
In reply to Gene Chen
Thank you for the patch! Yet something to improve:

Hi Gene,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on ljones-mfd/for-mfd-next]
[cannot apply to v5.4-rc4 next-20191024]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Gene-Chen/mfd-mt6360-add-pmic-mt6360-driver/20191024-182129
base:   https://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd.git for-mfd-next
config: mips-allmodconfig (attached as .config)
compiler: mips-linux-gcc (GCC) 7.4.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.4.0 make.cross ARCH=mips

If you fix the issue, kindly add following tag

All errors (new ones prefixed by >>):

In file included from <command-line>:0:0:
include/linux/mfd/mt6360-private.h: In function 'mt6360_pdata_apply_helper':

>> include/linux/mfd/mt6360-private.h:271:9: error: implicit declaration of function 'regmap_update_bits'; did you mean 'work_data_bits'? [-Werror=implicit-function-declaration]

ret = regmap_update_bits(context,
^~~~~~~~~~~~~~~~~~
work_data_bits
cc1: some warnings being treated as errors

vim +271 include/linux/mfd/mt6360-private.h

258
259 static inline int mt6360_pdata_apply_helper(void *context, void *pdata,
260 const struct mt6360_pdata_prop *prop,
261 int prop_cnt)
262 {
263 int i, ret;
264 u32 val;
265
266 for (i = 0; i < prop_cnt; i++) {
267 val = *(u32 *)(pdata + prop[i].offset);
268 if (prop[i].transform)
269 val = prop[i].transform(val);
270 val += prop[i].base;

 > 271			ret = regmap_update_bits(context,

272 prop[i].reg, prop[i].mask, val << prop[i].shift);
273 if (ret < 0)
274 return ret;
275 }
276 return 0;
277 }
278

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#5 lore raw
In reply to Gene Chen
Thank you for the patch! Yet something to improve:

Hi Gene,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on ljones-mfd/for-mfd-next]
[cannot apply to v5.4-rc4 next-20191024]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/Gene-Chen/mfd-mt6360-add-pmic-mt6360-driver/20191024-182129
base:   https://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd.git for-mfd-next
config: x86_64-allyesconfig (attached as .config)
compiler: gcc-7 (Debian 7.4.0-14) 7.4.0
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

If you fix the issue, kindly add following tag

All error/warnings (new ones prefixed by >>):

In file included from <command-line>:0:0:

>> include/linux/mfd/mt6360-private.h:234:51: warning: 'struct device_node' declared inside parameter list will not be visible outside of this definition or declaration
    static inline void mt6360_dt_parser_helper(struct device_node *np, void *data,
                                                      ^~~~~~~~~~~
   include/linux/mfd/mt6360-private.h: In function 'mt6360_dt_parser_helper':
>> include/linux/mfd/mt6360-private.h:243:3: error: implicit declaration of function 'of_property_read_u32' [-Werror=implicit-function-declaration]
      of_property_read_u32(np, props[i].name, data + props[i].offset);
      ^~~~~~~~~~~~~~~~~~~~

include/linux/mfd/mt6360-private.h: In function 'mt6360_pdata_apply_helper':
include/linux/mfd/mt6360-private.h:271:9: error: implicit declaration of function 'regmap_update_bits'; did you mean 'work_data_bits'? [-Werror=implicit-function-declaration]
ret = regmap_update_bits(context,
^~~~~~~~~~~~~~~~~~
work_data_bits
cc1: some warnings being treated as errors

vim +/of_property_read_u32 +243 include/linux/mfd/mt6360-private.h

233

 > 234	static inline void mt6360_dt_parser_helper(struct device_node *np, void *data,
   235						   const struct mt6360_val_prop *props,
   236						   int prop_cnt)
   237	{
   238		int i;
   239	
   240		for (i = 0; i < prop_cnt; i++) {
   241			if (unlikely(!props[i].name))
   242				continue;
 > 243			of_property_read_u32(np, props[i].name, data + props[i].offset);

244 }
245 }
246

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation