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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
/*
* combo redriver for ptn36502.c
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/clk.h>
#include <linux/i2c.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/sec_sysfs.h>
#include <linux/combo_redriver/ptn36502.h>
/*#define PTN36502_DEBUG*/
#ifdef PTN36502_DEBUG
#define i2c_smbus_write_byte_data(fmt, ...) \
do { \
i2c_smbus_write_byte_data(fmt, ##__VA_ARGS__); \
pr_info("%s :: address : 0x%x value: 0x%x \n", __func__, ##__VA_ARGS__); \
} while (0)
#endif
struct ptn36502_data *redrv_data;
struct device *combo_redriver_device;
void ptn36502_config(int config, int is_DFP)
{
int is_front = 0;
u8 value;
static int config_bak = INIT_MODE, is_DFP_bak;
if (!redrv_data) {
pr_err("redrv_data is NULL (config=%d, is_DFP=%d)\n", config, is_DFP);
config_bak = config;
is_DFP_bak = is_DFP;
return;
}
is_front = !gpio_get_value(redrv_data->con_sel);
pr_info("%s: config(%d)(%s)(%s)\n", __func__, config, (is_DFP ? "DFP":"UFP"), (is_front ? "FRONT":"REAR"));
switch (config) {
case BOOTING_INIT:
case INIT_MODE:
i2c_smbus_write_byte_data(redrv_data->i2c, Device_Control, 0x81);//chip reset
i2c_smbus_write_byte_data(redrv_data->i2c, Mode_Control, 0x48);
i2c_smbus_write_byte_data(redrv_data->i2c, DP_Link_Control, 0x0e);
i2c_smbus_write_byte_data(redrv_data->i2c, USB_TXRX_Control, redrv_data->usbControl_US.data);
pr_info("%s: usbControl_US as (%x) \n", __func__, redrv_data->usbControl_US.data);
i2c_smbus_write_byte_data(redrv_data->i2c, DS_TXRX_Control, redrv_data->usbControl_DS.data);
i2c_smbus_write_byte_data(redrv_data->i2c, DP_Lane0_Control, 0x29);
i2c_smbus_write_byte_data(redrv_data->i2c, DP_Lane1_Control, 0x29);
i2c_smbus_write_byte_data(redrv_data->i2c, DP_Lane2_Control, 0x29);
i2c_smbus_write_byte_data(redrv_data->i2c, DP_Lane3_Control, 0x29);
if (config == BOOTING_INIT && config_bak != INIT_MODE)
ptn36502_config(config_bak, is_DFP_bak);
break;
case USB3_ONLY_MODE:
if (is_DFP)
i2c_smbus_write_byte_data(redrv_data->i2c, Mode_Control, is_front ? 0x61:0x41);
else
i2c_smbus_write_byte_data(redrv_data->i2c, Mode_Control, is_front ? 0x81:0xa1);
value = i2c_smbus_read_byte_data(redrv_data->i2c, Mode_Control);
pr_info("%s: read 0x0b command as (%x) \n", __func__, value);
break;
case DP2_LANE_USB3_MODE:
i2c_smbus_write_byte_data(redrv_data->i2c, 0x7f, 0xd2);
i2c_smbus_write_byte_data(redrv_data->i2c, 0x7f, 0x9f);
i2c_smbus_write_byte_data(redrv_data->i2c, 0x7f, 0x1f);
i2c_smbus_write_byte_data(redrv_data->i2c, 0x6e, is_front ? 0xd9:0xc7);
i2c_smbus_write_byte_data(redrv_data->i2c, 0xff, 0x00);
if (is_DFP)
i2c_smbus_write_byte_data(redrv_data->i2c, Mode_Control, is_front ? 0x4a:0x6a);
else
i2c_smbus_write_byte_data(redrv_data->i2c, Mode_Control, is_front ? 0x8a:0xaa);
break;
case DP4_LANE_MODE:
i2c_smbus_write_byte_data(redrv_data->i2c, Mode_Control, is_front ? 0x68:0x48);
i2c_smbus_write_byte_data(redrv_data->i2c, Mode_Control, is_front ? 0x6b:0x4b);
#if 0
if (is_DFP)
i2c_smbus_write_byte_data(redrv_data->i2c, Mode_Control, is_front ? 0x48:0x68);
else
i2c_smbus_write_byte_data(redrv_data->i2c, Mode_Control, is_front ? 0x88:0xa8);
i2c_smbus_write_byte_data(redrv_data->i2c, 0x7f, 0xd2);
i2c_smbus_write_byte_data(redrv_data->i2c, 0x7f, 0x9f);
i2c_smbus_write_byte_data(redrv_data->i2c, 0x7f, 0x1f);
i2c_smbus_write_byte_data(redrv_data->i2c, 0x6e, is_front ? 0xd9:0xc7);
i2c_smbus_write_byte_data(redrv_data->i2c, 0xff, 0x00);
if (is_DFP)
i2c_smbus_write_byte_data(redrv_data->i2c, Mode_Control, is_front ? 0x4b:0x6b);
else
i2c_smbus_write_byte_data(redrv_data->i2c, Mode_Control, is_front ? 0x8b:0xab);
#endif
break;
}
}
EXPORT_SYMBOL(ptn36502_config);
static int ptn36502_set_gpios(struct device *dev)
{
struct device_node *np = dev->of_node;
int ret = 0;
redrv_data->combo_redriver_en = -1;
redrv_data->redriver_en = -1;
redrv_data->con_sel = 0;
redrv_data->combo_redriver_en = of_get_named_gpio(np, "combo,ptn_en", 0);
redrv_data->redriver_en = of_get_named_gpio(np, "combo,redriver_en", 0);
redrv_data->con_sel = of_get_named_gpio(np, "combo,con_sel", 0);
np = of_find_all_nodes(NULL);
if (gpio_is_valid(redrv_data->combo_redriver_en)) {
ret = gpio_request(redrv_data->combo_redriver_en, "ptn36502_en");
pr_info("%s: combo_redriver_en ret gpio_request (%d)\n", __func__, ret);
ret = gpio_direction_input(redrv_data->combo_redriver_en);
pr_info("%s: combo_redriver_en ret gpio_direction_input (%d)\n", __func__, ret);
} else
pr_info("%s: combo_redriver_en gpio is invalid\n", __func__);
msleep(1000);
if (gpio_is_valid(redrv_data->redriver_en)) {
ret = gpio_request(redrv_data->redriver_en, "ap7341d_redriver_en");
pr_info("%s: ap7341d_redriver_en ret gpio_request (%d)\n", __func__, ret);
ret = gpio_direction_output(redrv_data->redriver_en, 1);
pr_info("%s: ap7341d_redriver_en ret gpio_direction_output (%d)\n", __func__, ret);
} else
pr_info("%s: ap7341d_redriver_en gpio is invalid\n", __func__);
msleep(1000);
pr_info("%s: after set values (%d) (%d)\n",
__func__, gpio_get_value(redrv_data->combo_redriver_en), gpio_get_value(redrv_data->redriver_en));
return 0;
}
static void init_usb_control(void)
{
redrv_data->usbControl_US.BITS.RxEq = 2;
redrv_data->usbControl_US.BITS.Swing = 1;
redrv_data->usbControl_US.BITS.DeEmpha = 1;
pr_info("%s: usbControl_US (%x)\n", __func__, redrv_data->usbControl_US.data);
redrv_data->usbControl_DS.BITS.RxEq = 2;
redrv_data->usbControl_DS.BITS.Swing = 1;
redrv_data->usbControl_DS.BITS.DeEmpha = 1;
pr_info("%s: usbControl_DS (%x)\n", __func__, redrv_data->usbControl_DS.data);
}
static ssize_t ptn_us_tune_show(struct device *dev,
struct device_attribute *attr, char *buff)
{
return sprintf(buff, "US: 0x%x 0x%x 0x%x Total(0x%x)\n",
redrv_data->usbControl_US.BITS.RxEq,
redrv_data->usbControl_US.BITS.Swing,
redrv_data->usbControl_US.BITS.DeEmpha,
redrv_data->usbControl_US.data
);
}
static ssize_t ptn_us_tune_store(struct device *dev,
struct device_attribute *attr, const char *buff, size_t size)
{
char *name;
char *field;
char *value;
int tmp = 0;
char buf[256], *b, *c;
printk(KERN_DEBUG "%s buff=%s\n", __func__, buff);
strlcpy(buf, buff, sizeof(buf));
b = strim(buf);
while (b) {
name = strsep(&b, ",");
if (!name)
continue;
c = strim(name);
field = strsep(&c, "=");
value = strsep(&c, "=");
printk(KERN_DEBUG "usb: %s field=%s value=%s\n", __func__, field, value);
if (!strcmp(field, "eq")) {
sscanf(value, "%d\n", &tmp);
redrv_data->usbControl_US.BITS.RxEq = tmp;
printk(KERN_DEBUG "RxEq value=%d\n", redrv_data->usbControl_US.BITS.RxEq);
tmp = 0;
} else if (!strcmp(field, "swing")) {
sscanf(value, "%d\n", &tmp);
redrv_data->usbControl_US.BITS.Swing = tmp;
printk(KERN_DEBUG "Swing value=%d\n", redrv_data->usbControl_US.BITS.Swing);
tmp = 0;
} else if (!strcmp(field, "emp")) {
sscanf(value, "%d\n", &tmp);
redrv_data->usbControl_US.BITS.DeEmpha = tmp;
printk(KERN_DEBUG "DeEmpha value=%d\n", redrv_data->usbControl_US.BITS.DeEmpha);
tmp = 0;
}
}
return size;
}
static DEVICE_ATTR(ptn_us_tune, S_IRUGO | S_IWUSR, ptn_us_tune_show,
ptn_us_tune_store);
static ssize_t ptn_ds_tune_show(struct device *dev,
struct device_attribute *attr, char *buff)
{
return sprintf(buff, "DS : 0x%x 0x%x 0x%x Total(0x%x)\n",
redrv_data->usbControl_DS.BITS.RxEq,
redrv_data->usbControl_DS.BITS.Swing,
redrv_data->usbControl_DS.BITS.DeEmpha,
redrv_data->usbControl_DS.data
);
}
static ssize_t ptn_ds_tune_store(struct device *dev,
struct device_attribute *attr, const char *buff, size_t size)
{
char *name;
char *field;
char *value;
int tmp = 0;
char buf[256], *b, *c;
printk(KERN_DEBUG "%s buff=%s\n", __func__, buff);
strlcpy(buf, buff, sizeof(buf));
b = strim(buf);
while (b) {
name = strsep(&b, ",");
if (!name)
continue;
c = strim(name);
field = strsep(&c, "=");
value = strsep(&c, "=");
printk(KERN_DEBUG "usb: %s field=%s value=%s\n", __func__, field, value);
if (!strcmp(field, "eq")) {
sscanf(value, "%d\n", &tmp);
redrv_data->usbControl_DS.BITS.RxEq = tmp;
printk(KERN_DEBUG "RxEq value=%d\n", redrv_data->usbControl_DS.BITS.RxEq);
tmp = 0;
} else if (!strcmp(field, "swing")) {
sscanf(value, "%d\n", &tmp);
redrv_data->usbControl_DS.BITS.Swing = tmp;
printk(KERN_DEBUG "Swing value=%d\n", redrv_data->usbControl_DS.BITS.Swing);
tmp = 0;
} else if (!strcmp(field, "emp")) {
sscanf(value, "%d\n", &tmp);
redrv_data->usbControl_DS.BITS.DeEmpha = tmp;
printk(KERN_DEBUG "DeEmpha value=%d\n", redrv_data->usbControl_DS.BITS.DeEmpha);
tmp = 0;
}
}
return size;
}
static DEVICE_ATTR(ptn_ds_tune, S_IRUGO | S_IWUSR, ptn_ds_tune_show,
ptn_ds_tune_store);
static int ptn36502_probe(struct i2c_client *i2c,
const struct i2c_device_id *id)
{
struct i2c_adapter *adapter = to_i2c_adapter(i2c->dev.parent);
int ret = 0;
pr_info("%s\n", __func__);
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) {
dev_err(&i2c->dev, "i2c functionality check error\n");
return -EIO;
}
redrv_data = devm_kzalloc(&i2c->dev, sizeof(*redrv_data), GFP_KERNEL);
if (!redrv_data) {
dev_err(&i2c->dev, "Failed to allocate driver data\n");
return -ENOMEM;
}
if (i2c->dev.of_node)
ptn36502_set_gpios(&i2c->dev);
else
dev_err(&i2c->dev, "not found ptn36502 dt ret:%d\n", ret);
redrv_data->dev = &i2c->dev;
redrv_data->i2c = i2c;
i2c_set_clientdata(i2c, redrv_data);
mutex_init(&redrv_data->i2c_mutex);
init_usb_control();
ptn36502_config(BOOTING_INIT, 0);
return ret;
}
static int ptn36502_remove(struct i2c_client *i2c)
{
struct ptn36502_data *redrv_data = i2c_get_clientdata(i2c);
if (redrv_data->i2c) {
mutex_destroy(&redrv_data->i2c_mutex);
i2c_set_clientdata(redrv_data->i2c, NULL);
}
return 0;
}
static void ptn36502_shutdown(struct i2c_client *i2c)
{
;
}
#define PTN36502_DEV_NAME "ptn36502"
static const struct i2c_device_id ptn36502_id[] = {
{ PTN36502_DEV_NAME, 0 },
{}
};
MODULE_DEVICE_TABLE(i2c, ptn36502_id);
static struct of_device_id ptn36502_i2c_dt_ids[] = {
{ .compatible = "ptn36502_driver" },
{ }
};
static struct i2c_driver ptn36502_driver = {
.driver = {
.name = PTN36502_DEV_NAME,
.of_match_table = ptn36502_i2c_dt_ids,
},
.probe = ptn36502_probe,
.remove = ptn36502_remove,
.shutdown = ptn36502_shutdown,
.id_table = ptn36502_id,
};
static struct attribute *ptn36502_attributes[] = {
&dev_attr_ptn_us_tune.attr,
&dev_attr_ptn_ds_tune.attr,
NULL
};
const struct attribute_group ptn36502_sysfs_group = {
.attrs = ptn36502_attributes,
};
static int __init ptn36502_init(void)
{
int ret = 0;
ret = i2c_add_driver(&ptn36502_driver);
pr_info("%s ret is %d\n", __func__, ret);
combo_redriver_device = sec_device_create(NULL, "combo");
if (IS_ERR(combo_redriver_device))
pr_err("%s Failed to create device(combo)!\n", __func__);
ret = sysfs_create_group(&combo_redriver_device->kobj, &ptn36502_sysfs_group);
if (ret)
pr_err("%s: sysfs_create_group fail, ret %d", __func__, ret);
return ret;
}
module_init(ptn36502_init);
static void __exit ptn36502_exit(void)
{
i2c_del_driver(&ptn36502_driver);
}
module_exit(ptn36502_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("ptn36502 combo redriver driver");
MODULE_AUTHOR("lucky29.park <lucky29.park@samsung.com>");
|