1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Arizona-i2c.c  --  Arizona I2C bus interface
4 *
5 * Copyright 2012 Wolfson Microelectronics plc
6 *
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8 */
9
10#include <linux/err.h>
11#include <linux/i2c.h>
12#include <linux/module.h>
13#include <linux/pm_runtime.h>
14#include <linux/regmap.h>
15#include <linux/regulator/consumer.h>
16#include <linux/slab.h>
17#include <linux/of.h>
18
19#include <linux/mfd/arizona/core.h>
20
21#include "arizona.h"
22
23static int arizona_i2c_probe(struct i2c_client *i2c)
24{
25	const struct i2c_device_id *id = i2c_client_get_device_id(i2c);
26	const void *match_data;
27	struct arizona *arizona;
28	const struct regmap_config *regmap_config = NULL;
29	unsigned long type = 0;
30	int ret;
31
32	match_data = device_get_match_data(&i2c->dev);
33	if (match_data)
34		type = (unsigned long)match_data;
35	else if (id)
36		type = id->driver_data;
37
38	switch (type) {
39	case WM5102:
40		if (IS_ENABLED(CONFIG_MFD_WM5102))
41			regmap_config = &wm5102_i2c_regmap;
42		break;
43	case WM5110:
44	case WM8280:
45		if (IS_ENABLED(CONFIG_MFD_WM5110))
46			regmap_config = &wm5110_i2c_regmap;
47		break;
48	case WM8997:
49		if (IS_ENABLED(CONFIG_MFD_WM8997))
50			regmap_config = &wm8997_i2c_regmap;
51		break;
52	case WM8998:
53	case WM1814:
54		if (IS_ENABLED(CONFIG_MFD_WM8998))
55			regmap_config = &wm8998_i2c_regmap;
56		break;
57	default:
58		dev_err(&i2c->dev, "Unknown device type %ld\n", type);
59		return -EINVAL;
60	}
61
62	if (!regmap_config) {
63		dev_err(&i2c->dev,
64			"No kernel support for device type %ld\n", type);
65		return -EINVAL;
66	}
67
68	arizona = devm_kzalloc(&i2c->dev, sizeof(*arizona), GFP_KERNEL);
69	if (arizona == NULL)
70		return -ENOMEM;
71
72	arizona->regmap = devm_regmap_init_i2c(i2c, regmap_config);
73	if (IS_ERR(arizona->regmap)) {
74		ret = PTR_ERR(arizona->regmap);
75		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
76			ret);
77		return ret;
78	}
79
80	arizona->type = type;
81	arizona->dev = &i2c->dev;
82	arizona->irq = i2c->irq;
83
84	return arizona_dev_init(arizona);
85}
86
87static void arizona_i2c_remove(struct i2c_client *i2c)
88{
89	struct arizona *arizona = dev_get_drvdata(&i2c->dev);
90
91	arizona_dev_exit(arizona);
92}
93
94static const struct i2c_device_id arizona_i2c_id[] = {
95	{ "wm5102", WM5102 },
96	{ "wm5110", WM5110 },
97	{ "wm8280", WM8280 },
98	{ "wm8997", WM8997 },
99	{ "wm8998", WM8998 },
100	{ "wm1814", WM1814 },
101	{ }
102};
103MODULE_DEVICE_TABLE(i2c, arizona_i2c_id);
104
105#ifdef CONFIG_OF
106static const struct of_device_id arizona_i2c_of_match[] = {
107	{ .compatible = "wlf,wm5102", .data = (void *)WM5102 },
108	{ .compatible = "wlf,wm5110", .data = (void *)WM5110 },
109	{ .compatible = "wlf,wm8280", .data = (void *)WM8280 },
110	{ .compatible = "wlf,wm8997", .data = (void *)WM8997 },
111	{ .compatible = "wlf,wm8998", .data = (void *)WM8998 },
112	{ .compatible = "wlf,wm1814", .data = (void *)WM1814 },
113	{},
114};
115MODULE_DEVICE_TABLE(of, arizona_i2c_of_match);
116#endif
117
118static struct i2c_driver arizona_i2c_driver = {
119	.driver = {
120		.name	= "arizona",
121		.pm	= pm_ptr(&arizona_pm_ops),
122		.of_match_table	= of_match_ptr(arizona_i2c_of_match),
123	},
124	.probe		= arizona_i2c_probe,
125	.remove		= arizona_i2c_remove,
126	.id_table	= arizona_i2c_id,
127};
128
129module_i2c_driver(arizona_i2c_driver);
130
131MODULE_SOFTDEP("pre: arizona_ldo1");
132MODULE_DESCRIPTION("Arizona I2C bus interface");
133MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
134MODULE_LICENSE("GPL");
135