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