xref: /openbmc/linux/drivers/mfd/mc13xxx-i2c.c (revision 25879d7b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2009-2010 Creative Product Design
4  * Marc Reilly marc@cpdesign.com.au
5  */
6 
7 #include <linux/slab.h>
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <linux/mfd/core.h>
11 #include <linux/mfd/mc13xxx.h>
12 #include <linux/of.h>
13 #include <linux/of_device.h>
14 #include <linux/i2c.h>
15 #include <linux/err.h>
16 
17 #include "mc13xxx.h"
18 
19 static const struct i2c_device_id mc13xxx_i2c_device_id[] = {
20 	{
21 		.name = "mc13892",
22 		.driver_data = (kernel_ulong_t)&mc13xxx_variant_mc13892,
23 	}, {
24 		.name = "mc34708",
25 		.driver_data = (kernel_ulong_t)&mc13xxx_variant_mc34708,
26 	}, {
27 		/* sentinel */
28 	}
29 };
30 MODULE_DEVICE_TABLE(i2c, mc13xxx_i2c_device_id);
31 
32 static const struct of_device_id mc13xxx_dt_ids[] = {
33 	{
34 		.compatible = "fsl,mc13892",
35 		.data = &mc13xxx_variant_mc13892,
36 	}, {
37 		.compatible = "fsl,mc34708",
38 		.data = &mc13xxx_variant_mc34708,
39 	}, {
40 		/* sentinel */
41 	}
42 };
43 MODULE_DEVICE_TABLE(of, mc13xxx_dt_ids);
44 
45 static const struct regmap_config mc13xxx_regmap_i2c_config = {
46 	.reg_bits = 8,
47 	.val_bits = 24,
48 
49 	.max_register = MC13XXX_NUMREGS,
50 
51 	.cache_type = REGCACHE_NONE,
52 };
53 
54 static int mc13xxx_i2c_probe(struct i2c_client *client)
55 {
56 	const struct i2c_device_id *id = i2c_client_get_device_id(client);
57 	struct mc13xxx *mc13xxx;
58 	int ret;
59 
60 	mc13xxx = devm_kzalloc(&client->dev, sizeof(*mc13xxx), GFP_KERNEL);
61 	if (!mc13xxx)
62 		return -ENOMEM;
63 
64 	dev_set_drvdata(&client->dev, mc13xxx);
65 
66 	mc13xxx->irq = client->irq;
67 
68 	mc13xxx->regmap = devm_regmap_init_i2c(client,
69 					       &mc13xxx_regmap_i2c_config);
70 	if (IS_ERR(mc13xxx->regmap)) {
71 		ret = PTR_ERR(mc13xxx->regmap);
72 		dev_err(&client->dev, "Failed to initialize regmap: %d\n", ret);
73 		return ret;
74 	}
75 
76 	if (client->dev.of_node) {
77 		const struct of_device_id *of_id =
78 			of_match_device(mc13xxx_dt_ids, &client->dev);
79 		mc13xxx->variant = of_id->data;
80 	} else {
81 		mc13xxx->variant = (void *)id->driver_data;
82 	}
83 
84 	return mc13xxx_common_init(&client->dev);
85 }
86 
87 static void mc13xxx_i2c_remove(struct i2c_client *client)
88 {
89 	mc13xxx_common_exit(&client->dev);
90 }
91 
92 static struct i2c_driver mc13xxx_i2c_driver = {
93 	.id_table = mc13xxx_i2c_device_id,
94 	.driver = {
95 		.name = "mc13xxx",
96 		.of_match_table = mc13xxx_dt_ids,
97 	},
98 	.probe_new = mc13xxx_i2c_probe,
99 	.remove = mc13xxx_i2c_remove,
100 };
101 
102 static int __init mc13xxx_i2c_init(void)
103 {
104 	return i2c_add_driver(&mc13xxx_i2c_driver);
105 }
106 subsys_initcall(mc13xxx_i2c_init);
107 
108 static void __exit mc13xxx_i2c_exit(void)
109 {
110 	i2c_del_driver(&mc13xxx_i2c_driver);
111 }
112 module_exit(mc13xxx_i2c_exit);
113 
114 MODULE_DESCRIPTION("i2c driver for Freescale MC13XXX PMIC");
115 MODULE_AUTHOR("Marc Reilly <marc@cpdesign.com.au");
116 MODULE_LICENSE("GPL v2");
117