xref: /OK3568_Linux_fs/kernel/drivers/mfd/bcm590xx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Broadcom BCM590xx PMU
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright 2014 Linaro Limited
6*4882a593Smuzhiyun  * Author: Matt Porter <mporter@linaro.org>
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/err.h>
10*4882a593Smuzhiyun #include <linux/i2c.h>
11*4882a593Smuzhiyun #include <linux/init.h>
12*4882a593Smuzhiyun #include <linux/mfd/bcm590xx.h>
13*4882a593Smuzhiyun #include <linux/mfd/core.h>
14*4882a593Smuzhiyun #include <linux/module.h>
15*4882a593Smuzhiyun #include <linux/moduleparam.h>
16*4882a593Smuzhiyun #include <linux/of.h>
17*4882a593Smuzhiyun #include <linux/of_device.h>
18*4882a593Smuzhiyun #include <linux/regmap.h>
19*4882a593Smuzhiyun #include <linux/slab.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun static const struct mfd_cell bcm590xx_devs[] = {
22*4882a593Smuzhiyun 	{
23*4882a593Smuzhiyun 		.name = "bcm590xx-vregs",
24*4882a593Smuzhiyun 	},
25*4882a593Smuzhiyun };
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun static const struct regmap_config bcm590xx_regmap_config_pri = {
28*4882a593Smuzhiyun 	.reg_bits	= 8,
29*4882a593Smuzhiyun 	.val_bits	= 8,
30*4882a593Smuzhiyun 	.max_register	= BCM590XX_MAX_REGISTER_PRI,
31*4882a593Smuzhiyun 	.cache_type	= REGCACHE_RBTREE,
32*4882a593Smuzhiyun };
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun static const struct regmap_config bcm590xx_regmap_config_sec = {
35*4882a593Smuzhiyun 	.reg_bits	= 8,
36*4882a593Smuzhiyun 	.val_bits	= 8,
37*4882a593Smuzhiyun 	.max_register	= BCM590XX_MAX_REGISTER_SEC,
38*4882a593Smuzhiyun 	.cache_type	= REGCACHE_RBTREE,
39*4882a593Smuzhiyun };
40*4882a593Smuzhiyun 
bcm590xx_i2c_probe(struct i2c_client * i2c_pri,const struct i2c_device_id * id)41*4882a593Smuzhiyun static int bcm590xx_i2c_probe(struct i2c_client *i2c_pri,
42*4882a593Smuzhiyun 			      const struct i2c_device_id *id)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	struct bcm590xx *bcm590xx;
45*4882a593Smuzhiyun 	int ret;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	bcm590xx = devm_kzalloc(&i2c_pri->dev, sizeof(*bcm590xx), GFP_KERNEL);
48*4882a593Smuzhiyun 	if (!bcm590xx)
49*4882a593Smuzhiyun 		return -ENOMEM;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	i2c_set_clientdata(i2c_pri, bcm590xx);
52*4882a593Smuzhiyun 	bcm590xx->dev = &i2c_pri->dev;
53*4882a593Smuzhiyun 	bcm590xx->i2c_pri = i2c_pri;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	bcm590xx->regmap_pri = devm_regmap_init_i2c(i2c_pri,
56*4882a593Smuzhiyun 						 &bcm590xx_regmap_config_pri);
57*4882a593Smuzhiyun 	if (IS_ERR(bcm590xx->regmap_pri)) {
58*4882a593Smuzhiyun 		ret = PTR_ERR(bcm590xx->regmap_pri);
59*4882a593Smuzhiyun 		dev_err(&i2c_pri->dev, "primary regmap init failed: %d\n", ret);
60*4882a593Smuzhiyun 		return ret;
61*4882a593Smuzhiyun 	}
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun 	/* Secondary I2C slave address is the base address with A(2) asserted */
64*4882a593Smuzhiyun 	bcm590xx->i2c_sec = i2c_new_dummy_device(i2c_pri->adapter,
65*4882a593Smuzhiyun 					  i2c_pri->addr | BIT(2));
66*4882a593Smuzhiyun 	if (IS_ERR(bcm590xx->i2c_sec)) {
67*4882a593Smuzhiyun 		dev_err(&i2c_pri->dev, "failed to add secondary I2C device\n");
68*4882a593Smuzhiyun 		return PTR_ERR(bcm590xx->i2c_sec);
69*4882a593Smuzhiyun 	}
70*4882a593Smuzhiyun 	i2c_set_clientdata(bcm590xx->i2c_sec, bcm590xx);
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	bcm590xx->regmap_sec = devm_regmap_init_i2c(bcm590xx->i2c_sec,
73*4882a593Smuzhiyun 						&bcm590xx_regmap_config_sec);
74*4882a593Smuzhiyun 	if (IS_ERR(bcm590xx->regmap_sec)) {
75*4882a593Smuzhiyun 		ret = PTR_ERR(bcm590xx->regmap_sec);
76*4882a593Smuzhiyun 		dev_err(&bcm590xx->i2c_sec->dev,
77*4882a593Smuzhiyun 			"secondary regmap init failed: %d\n", ret);
78*4882a593Smuzhiyun 		goto err;
79*4882a593Smuzhiyun 	}
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	ret = devm_mfd_add_devices(&i2c_pri->dev, -1, bcm590xx_devs,
82*4882a593Smuzhiyun 				   ARRAY_SIZE(bcm590xx_devs), NULL, 0, NULL);
83*4882a593Smuzhiyun 	if (ret < 0) {
84*4882a593Smuzhiyun 		dev_err(&i2c_pri->dev, "failed to add sub-devices: %d\n", ret);
85*4882a593Smuzhiyun 		goto err;
86*4882a593Smuzhiyun 	}
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	return 0;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun err:
91*4882a593Smuzhiyun 	i2c_unregister_device(bcm590xx->i2c_sec);
92*4882a593Smuzhiyun 	return ret;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun static const struct of_device_id bcm590xx_of_match[] = {
96*4882a593Smuzhiyun 	{ .compatible = "brcm,bcm59056" },
97*4882a593Smuzhiyun 	{ }
98*4882a593Smuzhiyun };
99*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, bcm590xx_of_match);
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun static const struct i2c_device_id bcm590xx_i2c_id[] = {
102*4882a593Smuzhiyun 	{ "bcm59056" },
103*4882a593Smuzhiyun 	{ }
104*4882a593Smuzhiyun };
105*4882a593Smuzhiyun MODULE_DEVICE_TABLE(i2c, bcm590xx_i2c_id);
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun static struct i2c_driver bcm590xx_i2c_driver = {
108*4882a593Smuzhiyun 	.driver = {
109*4882a593Smuzhiyun 		   .name = "bcm590xx",
110*4882a593Smuzhiyun 		   .of_match_table = of_match_ptr(bcm590xx_of_match),
111*4882a593Smuzhiyun 	},
112*4882a593Smuzhiyun 	.probe = bcm590xx_i2c_probe,
113*4882a593Smuzhiyun 	.id_table = bcm590xx_i2c_id,
114*4882a593Smuzhiyun };
115*4882a593Smuzhiyun module_i2c_driver(bcm590xx_i2c_driver);
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun MODULE_AUTHOR("Matt Porter <mporter@linaro.org>");
118*4882a593Smuzhiyun MODULE_DESCRIPTION("BCM590xx multi-function driver");
119*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
120