extcon: axp288: Remove unused extcon_nb struct member
[linux-block.git] / drivers / extcon / extcon-axp288.c
CommitLineData
f0312378
RP
1/*
2 * extcon-axp288.c - X-Power AXP288 PMIC extcon cable detection driver
3 *
4 * Copyright (C) 2015 Intel Corporation
5 * Author: Ramakrishna Pallala <ramakrishna.pallala@intel.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <linux/module.h>
18#include <linux/kernel.h>
19#include <linux/io.h>
20#include <linux/slab.h>
21#include <linux/interrupt.h>
22#include <linux/platform_device.h>
23#include <linux/property.h>
f0312378 24#include <linux/notifier.h>
176aa360 25#include <linux/extcon-provider.h>
f0312378
RP
26#include <linux/regmap.h>
27#include <linux/gpio.h>
28#include <linux/gpio/consumer.h>
29#include <linux/mfd/axp20x.h>
30
31/* Power source status register */
32#define PS_STAT_VBUS_TRIGGER BIT(0)
33#define PS_STAT_BAT_CHRG_DIR BIT(2)
34#define PS_STAT_VBUS_ABOVE_VHOLD BIT(3)
35#define PS_STAT_VBUS_VALID BIT(4)
36#define PS_STAT_VBUS_PRESENT BIT(5)
37
38/* BC module global register */
39#define BC_GLOBAL_RUN BIT(0)
40#define BC_GLOBAL_DET_STAT BIT(2)
41#define BC_GLOBAL_DBP_TOUT BIT(3)
42#define BC_GLOBAL_VLGC_COM_SEL BIT(4)
43#define BC_GLOBAL_DCD_TOUT_MASK (BIT(6)|BIT(5))
44#define BC_GLOBAL_DCD_TOUT_300MS 0
45#define BC_GLOBAL_DCD_TOUT_100MS 1
46#define BC_GLOBAL_DCD_TOUT_500MS 2
47#define BC_GLOBAL_DCD_TOUT_900MS 3
48#define BC_GLOBAL_DCD_DET_SEL BIT(7)
49
50/* BC module vbus control and status register */
51#define VBUS_CNTL_DPDM_PD_EN BIT(4)
52#define VBUS_CNTL_DPDM_FD_EN BIT(5)
53#define VBUS_CNTL_FIRST_PO_STAT BIT(6)
54
55/* BC USB status register */
56#define USB_STAT_BUS_STAT_MASK (BIT(3)|BIT(2)|BIT(1)|BIT(0))
57#define USB_STAT_BUS_STAT_SHIFT 0
58#define USB_STAT_BUS_STAT_ATHD 0
59#define USB_STAT_BUS_STAT_CONN 1
60#define USB_STAT_BUS_STAT_SUSP 2
61#define USB_STAT_BUS_STAT_CONF 3
62#define USB_STAT_USB_SS_MODE BIT(4)
63#define USB_STAT_DEAD_BAT_DET BIT(6)
64#define USB_STAT_DBP_UNCFG BIT(7)
65
66/* BC detect status register */
67#define DET_STAT_MASK (BIT(7)|BIT(6)|BIT(5))
68#define DET_STAT_SHIFT 5
69#define DET_STAT_SDP 1
70#define DET_STAT_CDP 2
71#define DET_STAT_DCP 3
72
f0312378
RP
73enum axp288_extcon_reg {
74 AXP288_PS_STAT_REG = 0x00,
75 AXP288_PS_BOOT_REASON_REG = 0x02,
76 AXP288_BC_GLOBAL_REG = 0x2c,
77 AXP288_BC_VBUS_CNTL_REG = 0x2d,
78 AXP288_BC_USB_STAT_REG = 0x2e,
79 AXP288_BC_DET_STAT_REG = 0x2f,
f0312378
RP
80};
81
82enum axp288_mux_select {
83 EXTCON_GPIO_MUX_SEL_PMIC = 0,
84 EXTCON_GPIO_MUX_SEL_SOC,
85};
86
87enum axp288_extcon_irq {
88 VBUS_FALLING_IRQ = 0,
89 VBUS_RISING_IRQ,
90 MV_CHNG_IRQ,
91 BC_USB_CHNG_IRQ,
92 EXTCON_IRQ_END,
93};
94
73b6ecdb 95static const unsigned int axp288_extcon_cables[] = {
11eecf91
CC
96 EXTCON_CHG_USB_SDP,
97 EXTCON_CHG_USB_CDP,
98 EXTCON_CHG_USB_DCP,
5298b836 99 EXTCON_USB,
2a9de9c0 100 EXTCON_NONE,
f0312378
RP
101};
102
103struct axp288_extcon_info {
104 struct device *dev;
105 struct regmap *regmap;
106 struct regmap_irq_chip_data *regmap_irqc;
76884241 107 struct gpio_desc *gpio_mux_cntl;
f0312378
RP
108 int irq[EXTCON_IRQ_END];
109 struct extcon_dev *edev;
5d2199ea 110 unsigned int previous_cable;
f0312378
RP
111};
112
113/* Power up/down reason string array */
114static char *axp288_pwr_up_down_info[] = {
115 "Last wake caused by user pressing the power button",
116 "Last wake caused by a charger insertion",
117 "Last wake caused by a battery insertion",
118 "Last wake caused by SOC initiated global reset",
119 "Last wake caused by cold reset",
120 "Last shutdown caused by PMIC UVLO threshold",
121 "Last shutdown caused by SOC initiated cold off",
122 "Last shutdown caused by user pressing the power button",
123 NULL,
124};
125
126/*
127 * Decode and log the given "reset source indicator" (rsi)
128 * register and then clear it.
129 */
130static void axp288_extcon_log_rsi(struct axp288_extcon_info *info)
131{
132 char **rsi;
133 unsigned int val, i, clear_mask = 0;
134 int ret;
135
136 ret = regmap_read(info->regmap, AXP288_PS_BOOT_REASON_REG, &val);
137 for (i = 0, rsi = axp288_pwr_up_down_info; *rsi; rsi++, i++) {
138 if (val & BIT(i)) {
139 dev_dbg(info->dev, "%s\n", *rsi);
140 clear_mask |= BIT(i);
141 }
142 }
143
144 /* Clear the register value for next reboot (write 1 to clear bit) */
145 regmap_write(info->regmap, AXP288_PS_BOOT_REASON_REG, clear_mask);
146}
147
148static int axp288_handle_chrg_det_event(struct axp288_extcon_info *info)
149{
f0312378
RP
150 int ret, stat, cfg, pwr_stat;
151 u8 chrg_type;
5d2199ea 152 unsigned int cable = info->previous_cable;
f0312378
RP
153 bool vbus_attach = false;
154
155 ret = regmap_read(info->regmap, AXP288_PS_STAT_REG, &pwr_stat);
156 if (ret < 0) {
157 dev_err(info->dev, "failed to read vbus status\n");
158 return ret;
159 }
160
5757aca1 161 vbus_attach = (pwr_stat & PS_STAT_VBUS_VALID);
f0312378 162 if (!vbus_attach)
3fe1e0e2 163 goto no_vbus;
f0312378
RP
164
165 /* Check charger detection completion status */
166 ret = regmap_read(info->regmap, AXP288_BC_GLOBAL_REG, &cfg);
167 if (ret < 0)
168 goto dev_det_ret;
169 if (cfg & BC_GLOBAL_DET_STAT) {
170 dev_dbg(info->dev, "can't complete the charger detection\n");
171 goto dev_det_ret;
172 }
173
174 ret = regmap_read(info->regmap, AXP288_BC_DET_STAT_REG, &stat);
175 if (ret < 0)
176 goto dev_det_ret;
177
178 chrg_type = (stat & DET_STAT_MASK) >> DET_STAT_SHIFT;
179
180 switch (chrg_type) {
181 case DET_STAT_SDP:
525867db 182 dev_dbg(info->dev, "sdp cable is connected\n");
11eecf91 183 cable = EXTCON_CHG_USB_SDP;
f0312378
RP
184 break;
185 case DET_STAT_CDP:
525867db 186 dev_dbg(info->dev, "cdp cable is connected\n");
11eecf91 187 cable = EXTCON_CHG_USB_CDP;
f0312378
RP
188 break;
189 case DET_STAT_DCP:
525867db 190 dev_dbg(info->dev, "dcp cable is connected\n");
11eecf91 191 cable = EXTCON_CHG_USB_DCP;
f0312378
RP
192 break;
193 default:
194 dev_warn(info->dev,
195 "disconnect or unknown or ID event\n");
196 }
197
3fe1e0e2
HG
198no_vbus:
199 /*
200 * If VBUS is absent Connect D+/D- lines to PMIC for BC
201 * detection. Else connect them to SOC for USB communication.
202 */
203 if (info->gpio_mux_cntl)
204 gpiod_set_value(info->gpio_mux_cntl,
205 vbus_attach ? EXTCON_GPIO_MUX_SEL_SOC
206 : EXTCON_GPIO_MUX_SEL_PMIC);
207
5d2199ea 208 extcon_set_state_sync(info->edev, info->previous_cable, false);
5298b836
BW
209 if (info->previous_cable == EXTCON_CHG_USB_SDP)
210 extcon_set_state_sync(info->edev, EXTCON_USB, false);
211
5d2199ea
HG
212 if (vbus_attach) {
213 extcon_set_state_sync(info->edev, cable, vbus_attach);
5298b836
BW
214 if (cable == EXTCON_CHG_USB_SDP)
215 extcon_set_state_sync(info->edev, EXTCON_USB,
216 vbus_attach);
217
5d2199ea
HG
218 info->previous_cable = cable;
219 }
f0312378
RP
220
221 return 0;
222
223dev_det_ret:
224 if (ret < 0)
225 dev_err(info->dev, "failed to detect BC Mod\n");
226
227 return ret;
228}
229
230static irqreturn_t axp288_extcon_isr(int irq, void *data)
231{
232 struct axp288_extcon_info *info = data;
233 int ret;
234
235 ret = axp288_handle_chrg_det_event(info);
236 if (ret < 0)
237 dev_err(info->dev, "failed to handle the interrupt\n");
238
239 return IRQ_HANDLED;
240}
241
be174952 242static void axp288_extcon_enable(struct axp288_extcon_info *info)
f0312378 243{
f0312378
RP
244 regmap_update_bits(info->regmap, AXP288_BC_GLOBAL_REG,
245 BC_GLOBAL_RUN, 0);
f0312378
RP
246 /* Enable the charger detection logic */
247 regmap_update_bits(info->regmap, AXP288_BC_GLOBAL_REG,
248 BC_GLOBAL_RUN, BC_GLOBAL_RUN);
249}
250
251static int axp288_extcon_probe(struct platform_device *pdev)
252{
253 struct axp288_extcon_info *info;
254 struct axp20x_dev *axp20x = dev_get_drvdata(pdev->dev.parent);
76884241 255 struct axp288_extcon_pdata *pdata = pdev->dev.platform_data;
f0312378
RP
256 int ret, i, pirq, gpio;
257
258 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
259 if (!info)
260 return -ENOMEM;
261
262 info->dev = &pdev->dev;
263 info->regmap = axp20x->regmap;
264 info->regmap_irqc = axp20x->regmap_irqc;
5d2199ea 265 info->previous_cable = EXTCON_NONE;
76884241
HG
266 if (pdata)
267 info->gpio_mux_cntl = pdata->gpio_mux_cntl;
268
f0312378
RP
269 platform_set_drvdata(pdev, info);
270
271 axp288_extcon_log_rsi(info);
272
273 /* Initialize extcon device */
274 info->edev = devm_extcon_dev_allocate(&pdev->dev,
275 axp288_extcon_cables);
276 if (IS_ERR(info->edev)) {
277 dev_err(&pdev->dev, "failed to allocate memory for extcon\n");
278 return PTR_ERR(info->edev);
279 }
280
281 /* Register extcon device */
282 ret = devm_extcon_dev_register(&pdev->dev, info->edev);
283 if (ret) {
284 dev_err(&pdev->dev, "failed to register extcon device\n");
285 return ret;
286 }
287
f0312378 288 /* Set up gpio control for USB Mux */
76884241
HG
289 if (info->gpio_mux_cntl) {
290 gpio = desc_to_gpio(info->gpio_mux_cntl);
4bf27b70 291 ret = devm_gpio_request(&pdev->dev, gpio, "USB_MUX");
f0312378
RP
292 if (ret < 0) {
293 dev_err(&pdev->dev,
294 "failed to request the gpio=%d\n", gpio);
4bf27b70 295 return ret;
f0312378 296 }
76884241 297 gpiod_direction_output(info->gpio_mux_cntl,
f0312378
RP
298 EXTCON_GPIO_MUX_SEL_PMIC);
299 }
300
301 for (i = 0; i < EXTCON_IRQ_END; i++) {
302 pirq = platform_get_irq(pdev, i);
01e1429b
AY
303 if (pirq < 0)
304 return pirq;
305
f0312378
RP
306 info->irq[i] = regmap_irq_get_virq(info->regmap_irqc, pirq);
307 if (info->irq[i] < 0) {
308 dev_err(&pdev->dev,
309 "failed to get virtual interrupt=%d\n", pirq);
310 ret = info->irq[i];
4bf27b70 311 return ret;
f0312378
RP
312 }
313
314 ret = devm_request_threaded_irq(&pdev->dev, info->irq[i],
315 NULL, axp288_extcon_isr,
316 IRQF_ONESHOT | IRQF_NO_SUSPEND,
317 pdev->name, info);
318 if (ret) {
319 dev_err(&pdev->dev, "failed to request interrupt=%d\n",
320 info->irq[i]);
4bf27b70 321 return ret;
f0312378
RP
322 }
323 }
324
be174952
HG
325 /* Start charger cable type detection */
326 axp288_extcon_enable(info);
f0312378
RP
327
328 return 0;
f0312378
RP
329}
330
dd3a55fc
HG
331static const struct platform_device_id axp288_extcon_table[] = {
332 { .name = "axp288_extcon" },
333 {},
334};
335MODULE_DEVICE_TABLE(platform, axp288_extcon_table);
336
f0312378
RP
337static struct platform_driver axp288_extcon_driver = {
338 .probe = axp288_extcon_probe,
dd3a55fc 339 .id_table = axp288_extcon_table,
f0312378
RP
340 .driver = {
341 .name = "axp288_extcon",
342 },
343};
344module_platform_driver(axp288_extcon_driver);
345
346MODULE_AUTHOR("Ramakrishna Pallala <ramakrishna.pallala@intel.com>");
347MODULE_DESCRIPTION("X-Powers AXP288 extcon driver");
348MODULE_LICENSE("GPL v2");