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
|
/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/module.h>
#include <linux/of_gpio.h>
#include <linux/interrupt.h>
#include <linux/power_supply.h>
#include <linux/regulator/consumer.h>
#define TYPE_C_I2C_NAME "usb-type-c-ti"
#define TI_I2C_DELAY_MS 100
#define CCD_DEFAULT 0x0
#define CCD_MEDIUM 0x1
#define CCD_HIGH 0x3
#define CCD_MASK (0x30) /* charging current status */
#define MAX_CURRENT_BC1P2 500
#define MAX_CURRENT_MEDIUM 1500
#define MAX_CURRENT_HIGH 3000
#define TIUSB_1P8_VOL_MAX 1800000 /* uV */
#define TI_INTS_ATTACH_MASK (0xC0) /* current attach state interrupt */
#define TI_ATTACH_TO_DFP 0x2 /* configured as UFP attaches to DFP */
#define TI_STS_8_REG 0x8
#define TI_STS_9_REG 0x9
#define TI_INTS_STATUS BIT(4)
static bool disable_on_suspend;
module_param(disable_on_suspend , bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(disable_on_suspend,
"Whether to disable chip on suspend if state is not attached");
struct ti_usb_type_c {
struct i2c_client *client;
struct power_supply *usb_psy;
int max_current;
u8 attach_state;
u8 status_8_reg;
u8 status_9_reg;
int enb_gpio;
int enb_gpio_polarity;
struct regulator *i2c_1p8;
};
struct ti_usb_type_c *ti_usb;
static int tiusb_read_regdata(struct i2c_client *i2c)
{
int rc;
uint16_t saddr = i2c->addr;
u8 attach_state, mask = TI_INTS_ATTACH_MASK;
rc = i2c_smbus_read_byte_data(i2c, TI_STS_8_REG);
if (rc < 0)
return -EIO;
ti_usb->status_8_reg = rc;
rc = i2c_smbus_read_byte_data(i2c, TI_STS_9_REG);
if (rc < 0)
return -EIO;
ti_usb->status_9_reg = rc;
/* Clear interrupt */
rc = i2c_smbus_write_byte_data(i2c, TI_STS_9_REG, ti_usb->status_9_reg);
if (rc < 0)
return -EIO;
dev_dbg(&i2c->dev, "i2c read from 0x%x-[%x %x]\n", saddr,
ti_usb->status_8_reg, ti_usb->status_9_reg);
if (!(ti_usb->status_9_reg & TI_INTS_STATUS)) {
dev_err(&i2c->dev, "intr_status is 0!, ignore interrupt\n");
ti_usb->attach_state = false;
return -EINVAL;
}
attach_state = ti_usb->status_9_reg & mask;
ti_usb->attach_state = attach_state >> find_first_bit((void *)&mask, 8);
return rc;
}
static int tiusb_update_power_supply(struct power_supply *psy, int limit)
{
const union power_supply_propval ret = {limit,};
/* Update USB of max charging current (500 corresponds to bc1.2 */
if (psy->set_property)
return psy->set_property(psy,
POWER_SUPPLY_PROP_INPUT_CURRENT_MAX, &ret);
return -ENODEV;
}
static void tiusb_update_max_current(struct ti_usb_type_c *ti_usb)
{
u8 mask = CCD_MASK;
u8 shift = find_first_bit((void *)&mask, 8);
u8 chg_mode = ti_usb->status_8_reg & mask;
chg_mode >>= shift;
/* update to 0 if type-c UFP detached */
if (ti_usb->attach_state != TI_ATTACH_TO_DFP) {
dev_dbg(&ti_usb->client->dev, "attach_state: %x\n",
ti_usb->attach_state);
ti_usb->max_current = 0;
return;
}
switch (chg_mode) {
case CCD_DEFAULT:
ti_usb->max_current = MAX_CURRENT_BC1P2;
break;
case CCD_MEDIUM:
ti_usb->max_current = MAX_CURRENT_MEDIUM;
break;
case CCD_HIGH:
ti_usb->max_current = MAX_CURRENT_HIGH;
break;
default:
dev_dbg(&ti_usb->client->dev, "wrong chg mode %x\n", chg_mode);
ti_usb->max_current = 500;
}
dev_dbg(&ti_usb->client->dev, "chg mode: %x, mA:%u, attach: %x\n",
chg_mode, ti_usb->max_current, ti_usb->attach_state);
}
static irqreturn_t tiusb_irq(int irq, void *data)
{
int ret;
struct ti_usb_type_c *ti_usb = (struct ti_usb_type_c *)data;
ret = tiusb_read_regdata(ti_usb->client);
if (ret < 0)
goto out;
tiusb_update_max_current(ti_usb);
ret = tiusb_update_power_supply(ti_usb->usb_psy, ti_usb->max_current);
if (ret < 0)
dev_err(&ti_usb->client->dev, "failed to notify USB-%d\n", ret);
out:
return IRQ_HANDLED;
}
static int tiusb_gpio_config(struct ti_usb_type_c *ti, bool enable)
{
int ret = 0;
if (!enable) {
gpio_set_value(ti_usb->enb_gpio, !ti_usb->enb_gpio_polarity);
return 0;
}
ret = devm_gpio_request(&ti->client->dev, ti->enb_gpio,
"ti_typec_enb_gpio");
if (ret) {
pr_err("unable to request gpio [%d]\n", ti->enb_gpio);
return ret;
}
ret = gpio_direction_output(ti->enb_gpio, ti->enb_gpio_polarity);
if (ret) {
dev_err(&ti->client->dev, "set dir[%d] failed for gpio[%d]\n",
ti->enb_gpio_polarity, ti->enb_gpio);
return ret;
}
dev_dbg(&ti->client->dev, "set dir[%d] for gpio[%d]\n",
ti->enb_gpio_polarity, ti->enb_gpio);
gpio_set_value(ti->enb_gpio, ti->enb_gpio_polarity);
msleep(TI_I2C_DELAY_MS);
return ret;
}
static int tiusb_ldo_init(struct ti_usb_type_c *ti, bool init)
{
int rc = 0;
if (!init) {
regulator_set_voltage(ti->i2c_1p8, 0, TIUSB_1P8_VOL_MAX);
rc = regulator_disable(ti->i2c_1p8);
return rc;
}
ti->i2c_1p8 = devm_regulator_get(&ti->client->dev, "vdd_io");
if (IS_ERR(ti->i2c_1p8)) {
rc = PTR_ERR(ti->i2c_1p8);
dev_err(&ti->client->dev, "unable to get 1p8(%d)\n", rc);
return rc;
}
rc = regulator_set_voltage(ti->i2c_1p8, TIUSB_1P8_VOL_MAX,
TIUSB_1P8_VOL_MAX);
if (rc) {
dev_err(&ti->client->dev, "unable to set voltage(%d)\n", rc);
goto put_1p8;
}
rc = regulator_enable(ti->i2c_1p8);
if (rc) {
dev_err(&ti->client->dev, "unable to enable 1p8-reg(%d)\n", rc);
return rc;
}
return 0;
put_1p8:
regulator_set_voltage(ti->i2c_1p8, 0, TIUSB_1P8_VOL_MAX);
return rc;
}
static int tiusb_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
{
int ret;
struct power_supply *usb_psy;
struct device_node *np = i2c->dev.of_node;
enum of_gpio_flags flags;
usb_psy = power_supply_get_by_name("usb");
if (!usb_psy) {
dev_dbg(&i2c->dev, "USB power_supply not found, defer probe\n");
return -EPROBE_DEFER;
}
ti_usb = devm_kzalloc(&i2c->dev, sizeof(struct ti_usb_type_c),
GFP_KERNEL);
if (!ti_usb)
return -ENOMEM;
i2c_set_clientdata(i2c, ti_usb);
ti_usb->client = i2c;
ti_usb->usb_psy = usb_psy;
if (i2c->irq < 0) {
dev_err(&i2c->dev, "irq not defined (%d)\n", i2c->irq);
ret = -EINVAL;
goto out;
}
/* override with module-param */
if (!disable_on_suspend)
disable_on_suspend = of_property_read_bool(np,
"ti,disable-on-suspend");
ti_usb->enb_gpio = of_get_named_gpio_flags(np, "ti,enb-gpio", 0,
&flags);
if (!gpio_is_valid(ti_usb->enb_gpio)) {
dev_dbg(&i2c->dev, "enb gpio_get fail:%d\n", ti_usb->enb_gpio);
} else {
ti_usb->enb_gpio_polarity = !(flags & OF_GPIO_ACTIVE_LOW);
ret = tiusb_gpio_config(ti_usb, true);
if (ret)
goto out;
}
ret = tiusb_ldo_init(ti_usb, true);
if (ret) {
dev_err(&ti_usb->client->dev, "i2c ldo init failed\n");
goto gpio_disable;
}
ret = tiusb_read_regdata(i2c);
if (ret == -EIO) {
dev_err(&ti_usb->client->dev, "i2c access failed\n");
ret = -EPROBE_DEFER;
goto ldo_disable;
}
/* Update initial state to USB */
tiusb_irq(i2c->irq, ti_usb);
ret = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL, tiusb_irq,
IRQF_ONESHOT | IRQF_TRIGGER_FALLING,
TYPE_C_I2C_NAME, ti_usb);
if (ret) {
dev_err(&i2c->dev, "irq(%d) req failed-%d\n", i2c->irq, ret);
goto ldo_disable;
}
dev_dbg(&i2c->dev, "%s finished, addr:%d\n", __func__, i2c->addr);
return 0;
ldo_disable:
tiusb_ldo_init(ti_usb, false);
gpio_disable:
if (gpio_is_valid(ti_usb->enb_gpio))
tiusb_gpio_config(ti_usb, false);
out:
return ret;
}
static int tiusb_remove(struct i2c_client *i2c)
{
struct ti_usb_type_c *ti_usb = i2c_get_clientdata(i2c);
tiusb_ldo_init(ti_usb, false);
if (gpio_is_valid(ti_usb->enb_gpio))
tiusb_gpio_config(ti_usb, false);
devm_kfree(&i2c->dev, ti_usb);
return 0;
}
#ifdef CONFIG_PM_SLEEP
static int tiusb_i2c_suspend(struct device *dev)
{
struct i2c_client *i2c = to_i2c_client(dev);
struct ti_usb_type_c *ti = i2c_get_clientdata(i2c);
dev_dbg(dev, "ti_usb PM suspend.. attach(%d) disable(%d)\n",
ti->attach_state, disable_on_suspend);
disable_irq(ti->client->irq);
/* Keep type-c chip enabled during session */
if (ti->attach_state)
return 0;
regulator_set_voltage(ti->i2c_1p8, 0, TIUSB_1P8_VOL_MAX);
regulator_disable(ti->i2c_1p8);
if (disable_on_suspend)
gpio_set_value(ti->enb_gpio, !ti->enb_gpio_polarity);
return 0;
}
static int tiusb_i2c_resume(struct device *dev)
{
int rc;
struct i2c_client *i2c = to_i2c_client(dev);
struct ti_usb_type_c *ti = i2c_get_clientdata(i2c);
dev_dbg(dev, "ti_usb PM resume\n");
/* suspend was no-op, just re-enable interrupt */
if (ti->attach_state) {
enable_irq(ti->client->irq);
return 0;
}
if (disable_on_suspend) {
gpio_set_value(ti->enb_gpio, ti->enb_gpio_polarity);
msleep(TI_I2C_DELAY_MS);
}
rc = regulator_set_voltage(ti->i2c_1p8, TIUSB_1P8_VOL_MAX,
TIUSB_1P8_VOL_MAX);
if (rc)
dev_err(&ti->client->dev, "unable to set voltage(%d)\n", rc);
rc = regulator_enable(ti->i2c_1p8);
if (rc)
dev_err(&ti->client->dev, "unable to enable 1p8-reg(%d)\n", rc);
enable_irq(ti->client->irq);
return rc;
}
#endif
static SIMPLE_DEV_PM_OPS(tiusb_i2c_pm_ops, tiusb_i2c_suspend,
tiusb_i2c_resume);
static const struct i2c_device_id tiusb_id[] = {
{ TYPE_C_I2C_NAME, 0 },
{ }
};
MODULE_DEVICE_TABLE(i2c, tiusb_id);
#ifdef CONFIG_OF
static const struct of_device_id tiusb_of_match[] = {
{ .compatible = "ti,usb-type-c", },
{},
};
MODULE_DEVICE_TABLE(of, tiusb_of_match);
#endif
static struct i2c_driver tiusb_driver = {
.driver = {
.name = TYPE_C_I2C_NAME,
.of_match_table = of_match_ptr(tiusb_of_match),
.pm = &tiusb_i2c_pm_ops,
},
.probe = tiusb_probe,
.remove = tiusb_remove,
.id_table = tiusb_id,
};
module_i2c_driver(tiusb_driver);
MODULE_DESCRIPTION("TI TypeC Detection driver");
MODULE_LICENSE("GPL v2");
|