xref: /OK3568_Linux_fs/kernel/drivers/mfd/max96755f.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Maxim max96755f MFD driver
4  *
5  * Copyright (C) 2022 Rockchip Electronics Co. Ltd.
6  */
7 
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/i2c.h>
11 #include <linux/i2c-mux.h>
12 #include <linux/extcon-provider.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/regmap.h>
15 #include <linux/mfd/core.h>
16 #include <linux/regulator/consumer.h>
17 #include <linux/mfd/max96755f.h>
18 
19 static const struct mfd_cell max96755f_devs[] = {
20 	{
21 		.name = "max96755f-pinctrl",
22 		.of_compatible = "maxim,max96755f-pinctrl",
23 	}, {
24 		.name = "max96755f-bridge",
25 		.of_compatible = "maxim,max96755f-bridge",
26 	},
27 };
28 
29 static const unsigned int max96755f_cable[] = {
30 	EXTCON_JACK_VIDEO_OUT,
31 	EXTCON_NONE,
32 };
33 
max96755f_vid_sync_detected(struct max96755f * max96755f)34 static bool max96755f_vid_sync_detected(struct max96755f *max96755f)
35 {
36 	u32 det;
37 
38 	if (regmap_read(max96755f->regmap, 0x55d, &det))
39 		return false;
40 
41 	if ((!(det & VS_DET)) || (!(det & HS_DET)))
42 		return false;
43 
44 	return true;
45 }
46 
max96755f_volatile_reg(struct device * dev,unsigned int reg)47 static bool max96755f_volatile_reg(struct device *dev, unsigned int reg)
48 {
49 	switch (reg) {
50 	case 0x0002:
51 	case 0x0053:
52 	case 0x0057:
53 	case 0x02be ... 0x02fc:
54 	case 0x0311:
55 	case 0x032a:
56 	case 0x0330 ... 0x0331:
57 	case 0x0385 ... 0x0387:
58 	case 0x03a4 ... 0x03ae:
59 		return false;
60 	default:
61 		return true;
62 	}
63 }
64 
65 static const struct regmap_config max96755f_regmap_config = {
66 	.name = "max96755f",
67 	.reg_bits = 16,
68 	.val_bits = 8,
69 	.max_register = 0x1b17,
70 	.volatile_reg = max96755f_volatile_reg,
71 	.cache_type = REGCACHE_RBTREE,
72 };
73 
max96755f_select(struct i2c_mux_core * muxc,u32 chan)74 static int max96755f_select(struct i2c_mux_core *muxc, u32 chan)
75 {
76 	struct max96755f *max96755f = dev_get_drvdata(muxc->dev);
77 	u32 link_cfg, val;
78 	int ret;
79 
80 	regmap_update_bits(max96755f->regmap, 0x0001, DIS_REM_CC,
81 			   FIELD_PREP(DIS_REM_CC, 0));
82 
83 	if (!max96755f->split_mode)
84 		return 0;
85 
86 	regmap_read(max96755f->regmap, 0x0010, &link_cfg);
87 	if ((link_cfg & LINK_CFG) == SPLITTER_MODE)
88 		return 0;
89 
90 	if (chan == 0 && (link_cfg & LINK_CFG) != LINKA) {
91 		regmap_update_bits(max96755f->regmap, 0x0010,
92 				   RESET_ONESHOT | AUTO_LINK | LINK_CFG,
93 				   FIELD_PREP(RESET_ONESHOT, 1) |
94 				   FIELD_PREP(AUTO_LINK, 0) |
95 				   FIELD_PREP(LINK_CFG, LINKA));
96 	} else if (chan == 1 && (link_cfg & LINK_CFG) != LINKB) {
97 		regmap_update_bits(max96755f->regmap, 0x0010,
98 				   RESET_ONESHOT | AUTO_LINK | LINK_CFG,
99 				   FIELD_PREP(RESET_ONESHOT, 1) |
100 				   FIELD_PREP(AUTO_LINK, 0) |
101 				   FIELD_PREP(LINK_CFG, LINKB));
102 	}
103 
104 	ret = regmap_read_poll_timeout(max96755f->regmap, 0x0013, val,
105 				       val & LOCKED, 100,
106 				       50 * USEC_PER_MSEC);
107 	if (ret < 0) {
108 		dev_err(max96755f->dev, "GMSL2 link lock timeout\n");
109 		return ret;
110 	}
111 
112 	return 0;
113 }
114 
max96755f_deselect(struct i2c_mux_core * muxc,u32 chan)115 static int max96755f_deselect(struct i2c_mux_core *muxc, u32 chan)
116 {
117 	struct max96755f *max96755f = dev_get_drvdata(muxc->dev);
118 
119 	regmap_update_bits(max96755f->regmap, 0x0001, DIS_REM_CC,
120 			   FIELD_PREP(DIS_REM_CC, 1));
121 
122 	return 0;
123 }
124 
max96755f_power_off(void * data)125 static void max96755f_power_off(void *data)
126 {
127 	struct max96755f *max96755f = data;
128 
129 	if (max96755f->reset_gpio)
130 		gpiod_direction_output(max96755f->reset_gpio, 1);
131 
132 	if (max96755f->enable_gpio)
133 		gpiod_direction_output(max96755f->enable_gpio, 0);
134 
135 	if (max96755f->supply)
136 		regulator_disable(max96755f->supply);
137 }
138 
max96755f_power_on(struct max96755f * max96755f)139 static int max96755f_power_on(struct max96755f *max96755f)
140 {
141 	int ret;
142 
143 	if (max96755f_vid_sync_detected(max96755f)) {
144 		extcon_set_state(max96755f->extcon, EXTCON_JACK_VIDEO_OUT, true);
145 		return 0;
146 	}
147 
148 	if (max96755f->supply) {
149 		ret = regulator_enable(max96755f->supply);
150 		if (ret < 0)
151 			return ret;
152 	}
153 
154 	if (max96755f->enable_gpio) {
155 		gpiod_direction_output(max96755f->enable_gpio, 1);
156 		msleep(100);
157 	}
158 
159 	if (max96755f->reset_gpio) {
160 		gpiod_direction_output(max96755f->reset_gpio, 0);
161 		msleep(100);
162 		gpiod_direction_output(max96755f->reset_gpio, 1);
163 		msleep(100);
164 		gpiod_direction_output(max96755f->reset_gpio, 0);
165 		msleep(100);
166 	}
167 
168 	regmap_update_bits(max96755f->regmap, 0x0001, DIS_REM_CC,
169 			   FIELD_PREP(DIS_REM_CC, 1));
170 	return 0;
171 }
172 
line_fault_monitor_show(struct device * device,struct device_attribute * attr,char * buf)173 static ssize_t line_fault_monitor_show(struct device *device,
174 				       struct device_attribute *attr,
175 				       char *buf)
176 {
177 	struct max96755f *max96755f = dev_get_drvdata(device);
178 	u32 pu_lf, lf, status;
179 
180 	regmap_read(max96755f->regmap, 0x0005, &pu_lf);
181 
182 	/*
183 	 * Line-fault status of wire connected to LMN0/1 pin
184 	 *
185 	 * 0b000: Short to battery
186 	 * 0b001: Short to GND
187 	 * 0b010: Normal operation
188 	 * 0b011: Line open
189 	 * 0b1XX: Line-to-line short
190 	 */
191 	regmap_read(max96755f->regmap, 0x0026, &lf);
192 
193 	if (FIELD_GET(PU_LF0, pu_lf)) {
194 		status = (lf & LF_0);
195 		return sprintf(buf, "%d\n", status);
196 	}
197 
198 	if (FIELD_GET(PU_LF1, pu_lf)) {
199 		status = (lf & LF_1) >> 4;
200 		return sprintf(buf, "%d\n", status);
201 	}
202 
203 	return sprintf(buf, "%d\n", -EINVAL);
204 }
205 
206 static DEVICE_ATTR_RO(line_fault_monitor);
207 
patgen_mode_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)208 static ssize_t patgen_mode_store(struct device *dev,
209 				 struct device_attribute *attr,
210 				 const char *buf, size_t count)
211 {
212 	struct max96755f *max96755f = dev_get_drvdata(dev);
213 	u8 patgen_mode;
214 	int ret;
215 
216 	ret = kstrtou8(buf, 0, &patgen_mode);
217 	if (ret)
218 		return ret;
219 
220 	regmap_update_bits(max96755f->regmap, 0x01e5, PATGEN_MODE,
221 			   FIELD_PREP(PATGEN_MODE, patgen_mode));
222 
223 	return count;
224 }
225 static DEVICE_ATTR_WO(patgen_mode);
226 
227 static struct attribute *max96755f_attrs[] = {
228 	&dev_attr_line_fault_monitor.attr,
229 	&dev_attr_patgen_mode.attr,
230 	NULL
231 };
232 
233 static const struct attribute_group max96755f_attr_group = {
234 	.attrs = max96755f_attrs,
235 };
236 
max96755f_sysfs_add(struct max96755f * max96755f)237 static int max96755f_sysfs_add(struct max96755f *max96755f)
238 {
239 	struct device *dev = max96755f->dev;
240 	int ret;
241 	u32 ch;
242 
243 	ret = of_property_read_u32(dev->of_node, "line-fault-monitor", &ch);
244 	if (!ret)
245 		regmap_update_bits(max96755f->regmap, 0x0005,
246 				   PU_LF0 << ch, PU_LF0 << ch);
247 
248 	ret = devm_device_add_group(dev, &max96755f_attr_group);
249 	if (ret) {
250 		dev_err(dev, "failed to register sysfs. err: %d\n", ret);
251 		return ret;
252 	};
253 
254 	return 0;
255 }
256 
max96755f_i2c_probe(struct i2c_client * client)257 static int max96755f_i2c_probe(struct i2c_client *client)
258 {
259 	struct device *dev = &client->dev;
260 	struct device_node *child;
261 	struct max96755f *max96755f;
262 	unsigned int nr = 0;
263 	bool idle_disc;
264 	int ret;
265 
266 	for_each_available_child_of_node(dev->of_node, child) {
267 		if (!of_find_property(child, "reg", NULL))
268 			continue;
269 
270 		nr++;
271 	}
272 
273 	max96755f = devm_kzalloc(dev, sizeof(*max96755f), GFP_KERNEL);
274 	if (!max96755f)
275 		return -ENOMEM;
276 
277 	idle_disc = device_property_read_bool(dev, "i2c-mux-idle-disconnect");
278 
279 	max96755f->muxc = i2c_mux_alloc(client->adapter, dev, nr, 0,
280 					I2C_MUX_LOCKED, max96755f_select,
281 					idle_disc ? max96755f_deselect : NULL);
282 	if (!max96755f->muxc)
283 		return -ENOMEM;
284 
285 	if (nr == 2)
286 		max96755f->split_mode = true;
287 
288 	max96755f->dev = dev;
289 	i2c_set_clientdata(client, max96755f);
290 
291 	max96755f->supply = devm_regulator_get(dev, "power");
292 	if (IS_ERR(max96755f->supply))
293 		return dev_err_probe(dev, PTR_ERR(max96755f->supply),
294 				     "failed to get power supply\n");
295 
296 	max96755f->lock_gpio = devm_gpiod_get_optional(dev, "lock", GPIOD_IN);
297 	if (IS_ERR(max96755f->lock_gpio))
298 		return dev_err_probe(dev, PTR_ERR(max96755f->lock_gpio),
299 				     "failed to get lock GPIO\n");
300 
301 	max96755f->enable_gpio = devm_gpiod_get_optional(dev, "enable", GPIOD_ASIS);
302 	if (IS_ERR(max96755f->enable_gpio)) {
303 		return dev_err_probe(dev, PTR_ERR(max96755f->enable_gpio),
304 				     "failed to get enable GPIO\n");
305 	}
306 
307 	max96755f->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
308 	if (IS_ERR(max96755f->reset_gpio))
309 		return dev_err_probe(dev, PTR_ERR(max96755f->reset_gpio),
310 				     "failed to get reset GPIO\n");
311 
312 	max96755f->regmap = devm_regmap_init_i2c(client, &max96755f_regmap_config);
313 	if (IS_ERR(max96755f->regmap))
314 		return dev_err_probe(dev, PTR_ERR(max96755f->regmap),
315 				     "failed to initialize regmap");
316 
317 	max96755f->extcon = devm_extcon_dev_allocate(dev, max96755f_cable);
318 	if (IS_ERR(max96755f->extcon))
319 		return dev_err_probe(dev, PTR_ERR(max96755f->extcon),
320 				     "failed to allocate extcon device\n");
321 
322 	ret = devm_extcon_dev_register(dev, max96755f->extcon);
323 	if (ret)
324 		return dev_err_probe(dev, ret,
325 				     "failed to register extcon device\n");
326 
327 	ret = max96755f_power_on(max96755f);
328 	if (ret)
329 		return ret;
330 
331 	ret = devm_add_action_or_reset(dev, max96755f_power_off, max96755f);
332 	if (ret)
333 		return ret;
334 
335 	ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO, max96755f_devs,
336 				   ARRAY_SIZE(max96755f_devs), NULL, 0, NULL);
337 	if (ret)
338 		return ret;
339 
340 	for_each_available_child_of_node(dev->of_node, child) {
341 		if (of_property_read_u32(child, "reg", &nr))
342 			continue;
343 
344 		ret = i2c_mux_add_adapter(max96755f->muxc, 0, nr, 0);
345 		if (ret) {
346 			i2c_mux_del_adapters(max96755f->muxc);
347 			return ret;
348 		}
349 	}
350 
351 	ret = max96755f_sysfs_add(max96755f);
352 	if (ret)
353 		return dev_err_probe(dev, ret, "failed to registers sysfs\n");
354 
355 	return 0;
356 }
357 
max96755f_i2c_remove(struct i2c_client * client)358 static int max96755f_i2c_remove(struct i2c_client *client)
359 {
360 	struct max96755f *max96755f = i2c_get_clientdata(client);
361 
362 	i2c_mux_del_adapters(max96755f->muxc);
363 
364 	return 0;
365 }
366 
max96755f_i2c_shutdown(struct i2c_client * client)367 static void max96755f_i2c_shutdown(struct i2c_client *client)
368 {
369 	struct max96755f *max96755f = i2c_get_clientdata(client);
370 
371 	max96755f_power_off(max96755f);
372 }
373 
max96755f_suspend(struct device * dev)374 static int __maybe_unused max96755f_suspend(struct device *dev)
375 {
376 	struct max96755f *max96755f = dev_get_drvdata(dev);
377 
378 	regcache_mark_dirty(max96755f->regmap);
379 	regcache_cache_only(max96755f->regmap, true);
380 
381 	return 0;
382 }
383 
max96755f_resume(struct device * dev)384 static int __maybe_unused max96755f_resume(struct device *dev)
385 {
386 	struct max96755f *max96755f = dev_get_drvdata(dev);
387 
388 	regcache_cache_only(max96755f->regmap, false);
389 	regcache_sync(max96755f->regmap);
390 
391 	return 0;
392 }
393 
394 static SIMPLE_DEV_PM_OPS(max96755f_pm_ops, max96755f_suspend, max96755f_resume);
395 
396 static const struct of_device_id max96755f_of_match[] = {
397 	{ .compatible = "maxim,max96755f", },
398 	{}
399 };
400 MODULE_DEVICE_TABLE(of, max96755f_of_match);
401 
402 static struct i2c_driver max96755f_i2c_driver = {
403 	.driver = {
404 		.name = "max96755f",
405 		.of_match_table = max96755f_of_match,
406 		.pm = &max96755f_pm_ops,
407 	},
408 	.probe_new = max96755f_i2c_probe,
409 	.remove = max96755f_i2c_remove,
410 	.shutdown = max96755f_i2c_shutdown,
411 };
412 
413 module_i2c_driver(max96755f_i2c_driver);
414 
415 MODULE_AUTHOR("Guochun Huang<hero.huang@rock-chips.com>");
416 MODULE_DESCRIPTION("Maxim max96755f MFD driver");
417 MODULE_LICENSE("GPL");
418