1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Maxim MAX96745 MFD driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2022 Rockchip Electronics Co. Ltd.
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <linux/module.h>
9*4882a593Smuzhiyun #include <linux/init.h>
10*4882a593Smuzhiyun #include <linux/i2c.h>
11*4882a593Smuzhiyun #include <linux/i2c-mux.h>
12*4882a593Smuzhiyun #include <linux/extcon-provider.h>
13*4882a593Smuzhiyun #include <linux/gpio/consumer.h>
14*4882a593Smuzhiyun #include <linux/regmap.h>
15*4882a593Smuzhiyun #include <linux/mfd/core.h>
16*4882a593Smuzhiyun #include <linux/mfd/max96745.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun static const struct mfd_cell max96745_devs[] = {
19*4882a593Smuzhiyun {
20*4882a593Smuzhiyun .name = "max96745-pinctrl",
21*4882a593Smuzhiyun .of_compatible = "maxim,max96745-pinctrl",
22*4882a593Smuzhiyun }, {
23*4882a593Smuzhiyun .name = "max96745-bridge",
24*4882a593Smuzhiyun .of_compatible = "maxim,max96745-bridge",
25*4882a593Smuzhiyun },
26*4882a593Smuzhiyun };
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun static const unsigned int max96745_cable[] = {
29*4882a593Smuzhiyun EXTCON_JACK_VIDEO_OUT,
30*4882a593Smuzhiyun EXTCON_NONE,
31*4882a593Smuzhiyun };
32*4882a593Smuzhiyun
max96745_vid_tx_active(struct max96745 * max96745)33*4882a593Smuzhiyun static bool max96745_vid_tx_active(struct max96745 *max96745)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun u32 val;
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun if (regmap_read(max96745->regmap, 0x0107, &val))
38*4882a593Smuzhiyun return false;
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun if (!FIELD_GET(VID_TX_ACTIVE_A | VID_TX_ACTIVE_B, val))
41*4882a593Smuzhiyun return false;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun return true;
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun
max96745_volatile_reg(struct device * dev,unsigned int reg)46*4882a593Smuzhiyun static bool max96745_volatile_reg(struct device *dev, unsigned int reg)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun switch (reg) {
49*4882a593Smuzhiyun case 0x0076:
50*4882a593Smuzhiyun case 0x0086:
51*4882a593Smuzhiyun case 0x0100:
52*4882a593Smuzhiyun case 0x0200 ... 0x02ce:
53*4882a593Smuzhiyun case 0x7000:
54*4882a593Smuzhiyun case 0x7070:
55*4882a593Smuzhiyun case 0x7074:
56*4882a593Smuzhiyun return false;
57*4882a593Smuzhiyun default:
58*4882a593Smuzhiyun return true;
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun static const struct regmap_config max96745_regmap_config = {
63*4882a593Smuzhiyun .name = "max96745",
64*4882a593Smuzhiyun .reg_bits = 16,
65*4882a593Smuzhiyun .val_bits = 8,
66*4882a593Smuzhiyun .max_register = 0x8000,
67*4882a593Smuzhiyun .volatile_reg = max96745_volatile_reg,
68*4882a593Smuzhiyun .cache_type = REGCACHE_RBTREE,
69*4882a593Smuzhiyun };
70*4882a593Smuzhiyun
max96745_select(struct i2c_mux_core * muxc,u32 chan)71*4882a593Smuzhiyun static int max96745_select(struct i2c_mux_core *muxc, u32 chan)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun struct max96745 *max96745 = dev_get_drvdata(muxc->dev);
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun if (!max96745->idle_disc)
76*4882a593Smuzhiyun return 0;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun if (chan == 1)
79*4882a593Smuzhiyun regmap_update_bits(max96745->regmap, 0x0086, DIS_REM_CC,
80*4882a593Smuzhiyun FIELD_PREP(DIS_REM_CC, 0));
81*4882a593Smuzhiyun else
82*4882a593Smuzhiyun regmap_update_bits(max96745->regmap, 0x0076, DIS_REM_CC,
83*4882a593Smuzhiyun FIELD_PREP(DIS_REM_CC, 0));
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun return 0;
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun
max96745_deselect(struct i2c_mux_core * muxc,u32 chan)88*4882a593Smuzhiyun static int max96745_deselect(struct i2c_mux_core *muxc, u32 chan)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun struct max96745 *max96745 = dev_get_drvdata(muxc->dev);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun if (!max96745->idle_disc)
93*4882a593Smuzhiyun return 0;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun if (chan == 1)
96*4882a593Smuzhiyun regmap_update_bits(max96745->regmap, 0x0086, DIS_REM_CC,
97*4882a593Smuzhiyun FIELD_PREP(DIS_REM_CC, 1));
98*4882a593Smuzhiyun else
99*4882a593Smuzhiyun regmap_update_bits(max96745->regmap, 0x0076, DIS_REM_CC,
100*4882a593Smuzhiyun FIELD_PREP(DIS_REM_CC, 1));
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun return 0;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
max96745_power_off(void * data)105*4882a593Smuzhiyun static void max96745_power_off(void *data)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun struct max96745 *max96745 = data;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun if (max96745->pwdnb_gpio)
110*4882a593Smuzhiyun gpiod_direction_output(max96745->pwdnb_gpio, 1);
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun if (max96745->enable_gpio)
113*4882a593Smuzhiyun gpiod_direction_output(max96745->enable_gpio, 0);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
max96745_power_on(struct max96745 * max96745)116*4882a593Smuzhiyun static void max96745_power_on(struct max96745 *max96745)
117*4882a593Smuzhiyun {
118*4882a593Smuzhiyun if (max96745_vid_tx_active(max96745)) {
119*4882a593Smuzhiyun extcon_set_state(max96745->extcon, EXTCON_JACK_VIDEO_OUT, true);
120*4882a593Smuzhiyun return;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun if (max96745->enable_gpio) {
124*4882a593Smuzhiyun gpiod_direction_output(max96745->enable_gpio, 1);
125*4882a593Smuzhiyun msleep(200);
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun if (max96745->pwdnb_gpio) {
129*4882a593Smuzhiyun gpiod_direction_output(max96745->pwdnb_gpio, 0);
130*4882a593Smuzhiyun msleep(30);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun /* Set for I2C Fast-mode speed */
134*4882a593Smuzhiyun regmap_write(max96745->regmap, 0x0070, 0x16);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun if (max96745->idle_disc) {
137*4882a593Smuzhiyun regmap_update_bits(max96745->regmap, 0x0076, DIS_REM_CC,
138*4882a593Smuzhiyun FIELD_PREP(DIS_REM_CC, 1));
139*4882a593Smuzhiyun regmap_update_bits(max96745->regmap, 0x0086, DIS_REM_CC,
140*4882a593Smuzhiyun FIELD_PREP(DIS_REM_CC, 1));
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
line_fault_monitor_show(struct device * device,struct device_attribute * attr,char * buf)144*4882a593Smuzhiyun static ssize_t line_fault_monitor_show(struct device *device,
145*4882a593Smuzhiyun struct device_attribute *attr,
146*4882a593Smuzhiyun char *buf)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun struct max96745 *max96745 = dev_get_drvdata(device);
149*4882a593Smuzhiyun u32 pu_lf, lf, status;
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun regmap_read(max96745->regmap, 0x0005, &pu_lf);
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /*
154*4882a593Smuzhiyun * Line-fault status of wire connected to LMN0/1/2/3 pin
155*4882a593Smuzhiyun *
156*4882a593Smuzhiyun * 0b000: Short to battery
157*4882a593Smuzhiyun * 0b001: Short to GND
158*4882a593Smuzhiyun * 0b010: Normal operation
159*4882a593Smuzhiyun * 0b011: Line open
160*4882a593Smuzhiyun * 0b1XX: Line-to-line short
161*4882a593Smuzhiyun */
162*4882a593Smuzhiyun regmap_read(max96745->regmap, 0x0026, &lf);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun if (FIELD_GET(PU_LF0, pu_lf)) {
165*4882a593Smuzhiyun status = (lf & LF_0);
166*4882a593Smuzhiyun return sprintf(buf, "%d\n", status);
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun if (FIELD_GET(PU_LF1, pu_lf)) {
170*4882a593Smuzhiyun status = (lf & LF_1) >> 4;
171*4882a593Smuzhiyun return sprintf(buf, "%d\n", status);
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun regmap_read(max96745->regmap, 0x0027, &lf);
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun if (FIELD_GET(PU_LF2, pu_lf)) {
177*4882a593Smuzhiyun status = (lf & LF_2);
178*4882a593Smuzhiyun return sprintf(buf, "%d\n", status);
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun if (FIELD_GET(PU_LF3, pu_lf)) {
182*4882a593Smuzhiyun status = (lf & LF_3) >> 4;
183*4882a593Smuzhiyun return sprintf(buf, "%d\n", status);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun return sprintf(buf, "%d\n", -EINVAL);
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun static DEVICE_ATTR_RO(line_fault_monitor);
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun static struct attribute *max96745_attrs[] = {
193*4882a593Smuzhiyun &dev_attr_line_fault_monitor.attr,
194*4882a593Smuzhiyun NULL
195*4882a593Smuzhiyun };
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun static const struct attribute_group max96745_attr_group = {
198*4882a593Smuzhiyun .attrs = max96745_attrs,
199*4882a593Smuzhiyun };
200*4882a593Smuzhiyun
max96745_sysfs_add(struct max96745 * max96745)201*4882a593Smuzhiyun static int max96745_sysfs_add(struct max96745 *max96745)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun struct device *dev = max96745->dev;
204*4882a593Smuzhiyun int ret;
205*4882a593Smuzhiyun u32 ch;
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun ret = of_property_read_u32(dev->of_node, "line-fault-monitor", &ch);
208*4882a593Smuzhiyun if (!ret)
209*4882a593Smuzhiyun regmap_update_bits(max96745->regmap, 0x0005,
210*4882a593Smuzhiyun PU_LF0 << ch, PU_LF0 << ch);
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun ret = devm_device_add_group(dev, &max96745_attr_group);
213*4882a593Smuzhiyun if (ret) {
214*4882a593Smuzhiyun dev_err(dev, "failed to register sysfs. err: %d\n", ret);
215*4882a593Smuzhiyun return ret;
216*4882a593Smuzhiyun };
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun return 0;
219*4882a593Smuzhiyun }
220*4882a593Smuzhiyun
max96745_i2c_probe(struct i2c_client * client)221*4882a593Smuzhiyun static int max96745_i2c_probe(struct i2c_client *client)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun struct device *dev = &client->dev;
224*4882a593Smuzhiyun struct device_node *child;
225*4882a593Smuzhiyun struct max96745 *max96745;
226*4882a593Smuzhiyun unsigned int nr = 0;
227*4882a593Smuzhiyun int ret;
228*4882a593Smuzhiyun
229*4882a593Smuzhiyun for_each_available_child_of_node(dev->of_node, child) {
230*4882a593Smuzhiyun if (!of_find_property(child, "reg", NULL))
231*4882a593Smuzhiyun continue;
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun nr++;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun max96745 = devm_kzalloc(dev, sizeof(*max96745), GFP_KERNEL);
237*4882a593Smuzhiyun if (!max96745)
238*4882a593Smuzhiyun return -ENOMEM;
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun max96745->idle_disc = device_property_read_bool(dev, "i2c-mux-idle-disconnect");
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun max96745->muxc = i2c_mux_alloc(client->adapter, dev, nr,
243*4882a593Smuzhiyun 0, I2C_MUX_LOCKED, max96745_select,
244*4882a593Smuzhiyun max96745_deselect);
245*4882a593Smuzhiyun if (!max96745->muxc)
246*4882a593Smuzhiyun return -ENOMEM;
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun max96745->dev = dev;
249*4882a593Smuzhiyun i2c_set_clientdata(client, max96745);
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun max96745->regmap = devm_regmap_init_i2c(client, &max96745_regmap_config);
252*4882a593Smuzhiyun if (IS_ERR(max96745->regmap))
253*4882a593Smuzhiyun return dev_err_probe(dev, PTR_ERR(max96745->regmap),
254*4882a593Smuzhiyun "failed to initialize regmap");
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun max96745->enable_gpio = devm_gpiod_get_optional(dev, "enable",
257*4882a593Smuzhiyun GPIOD_ASIS);
258*4882a593Smuzhiyun if (IS_ERR(max96745->enable_gpio))
259*4882a593Smuzhiyun return dev_err_probe(dev, PTR_ERR(max96745->enable_gpio),
260*4882a593Smuzhiyun "failed to get enable GPIO\n");
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun max96745->pwdnb_gpio = devm_gpiod_get_optional(dev, "pwdnb",
263*4882a593Smuzhiyun GPIOD_ASIS);
264*4882a593Smuzhiyun if (IS_ERR(max96745->pwdnb_gpio))
265*4882a593Smuzhiyun return dev_err_probe(dev, PTR_ERR(max96745->pwdnb_gpio),
266*4882a593Smuzhiyun "failed to get pwdnb GPIO\n");
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun max96745->extcon = devm_extcon_dev_allocate(dev, max96745_cable);
269*4882a593Smuzhiyun if (IS_ERR(max96745->extcon))
270*4882a593Smuzhiyun return dev_err_probe(dev, PTR_ERR(max96745->extcon),
271*4882a593Smuzhiyun "failed to allocate extcon device\n");
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun ret = devm_extcon_dev_register(dev, max96745->extcon);
274*4882a593Smuzhiyun if (ret)
275*4882a593Smuzhiyun return dev_err_probe(dev, ret,
276*4882a593Smuzhiyun "failed to register extcon device\n");
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun max96745_power_on(max96745);
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun ret = devm_add_action_or_reset(dev, max96745_power_off, max96745);
281*4882a593Smuzhiyun if (ret)
282*4882a593Smuzhiyun return ret;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO, max96745_devs,
285*4882a593Smuzhiyun ARRAY_SIZE(max96745_devs), NULL, 0, NULL);
286*4882a593Smuzhiyun if (ret)
287*4882a593Smuzhiyun return ret;
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun for_each_available_child_of_node(dev->of_node, child) {
290*4882a593Smuzhiyun if (of_property_read_u32(child, "reg", &nr))
291*4882a593Smuzhiyun continue;
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun ret = i2c_mux_add_adapter(max96745->muxc, 0, nr, 0);
294*4882a593Smuzhiyun if (ret) {
295*4882a593Smuzhiyun i2c_mux_del_adapters(max96745->muxc);
296*4882a593Smuzhiyun return ret;
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun ret = max96745_sysfs_add(max96745);
301*4882a593Smuzhiyun if (ret)
302*4882a593Smuzhiyun return dev_err_probe(dev, ret, "failed to registers sysfs\n");
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun return 0;
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun
max96745_i2c_remove(struct i2c_client * client)307*4882a593Smuzhiyun static int max96745_i2c_remove(struct i2c_client *client)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun struct max96745 *max96745 = i2c_get_clientdata(client);
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun i2c_mux_del_adapters(max96745->muxc);
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun return 0;
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
max96745_i2c_shutdown(struct i2c_client * client)316*4882a593Smuzhiyun static void max96745_i2c_shutdown(struct i2c_client *client)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun struct max96745 *max96745 = i2c_get_clientdata(client);
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun max96745_power_off(max96745);
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
max96745_suspend(struct device * dev)323*4882a593Smuzhiyun static int __maybe_unused max96745_suspend(struct device *dev)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun struct max96745 *max96745 = dev_get_drvdata(dev);
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun regcache_mark_dirty(max96745->regmap);
328*4882a593Smuzhiyun regcache_cache_only(max96745->regmap, true);
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun return 0;
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun
max96745_resume(struct device * dev)333*4882a593Smuzhiyun static int __maybe_unused max96745_resume(struct device *dev)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun struct max96745 *max96745 = dev_get_drvdata(dev);
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun regcache_cache_only(max96745->regmap, false);
338*4882a593Smuzhiyun regcache_sync(max96745->regmap);
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun return 0;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun static SIMPLE_DEV_PM_OPS(max96745_pm_ops, max96745_suspend, max96745_resume);
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun static const struct of_device_id max96745_of_match[] = {
346*4882a593Smuzhiyun { .compatible = "maxim,max96745", },
347*4882a593Smuzhiyun {}
348*4882a593Smuzhiyun };
349*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, max96745_of_match);
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun static struct i2c_driver max96745_i2c_driver = {
352*4882a593Smuzhiyun .driver = {
353*4882a593Smuzhiyun .name = "max96745",
354*4882a593Smuzhiyun .of_match_table = max96745_of_match,
355*4882a593Smuzhiyun .pm = &max96745_pm_ops,
356*4882a593Smuzhiyun },
357*4882a593Smuzhiyun .probe_new = max96745_i2c_probe,
358*4882a593Smuzhiyun .remove = max96745_i2c_remove,
359*4882a593Smuzhiyun .shutdown = max96745_i2c_shutdown,
360*4882a593Smuzhiyun };
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun module_i2c_driver(max96745_i2c_driver);
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun MODULE_AUTHOR("Wyon Bi <bivvy.bi@rock-chips.com>");
365*4882a593Smuzhiyun MODULE_DESCRIPTION("Maxim MAX96745 MFD driver");
366*4882a593Smuzhiyun MODULE_LICENSE("GPL");
367