blob: f2a7d970388f179be4450fa90e6c567dc037d413 [file] [log] [blame]
Heikki Krogerusec464752010-08-19 15:09:36 +03001/*
2 * ISP1704 USB Charger Detection driver
3 *
4 * Copyright (C) 2010 Nokia Corporation
Pali Rohárf07c11e2013-09-08 10:50:37 +02005 * Copyright (C) 2012 - 2013 Pali Rohár <pali.rohar@gmail.com>
Heikki Krogerusec464752010-08-19 15:09:36 +03006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#include <linux/kernel.h>
23#include <linux/module.h>
24#include <linux/err.h>
25#include <linux/init.h>
26#include <linux/types.h>
27#include <linux/device.h>
28#include <linux/sysfs.h>
29#include <linux/platform_device.h>
30#include <linux/power_supply.h>
31#include <linux/delay.h>
Sebastian Reichel34a10962013-12-01 02:00:10 +010032#include <linux/of.h>
33#include <linux/of_gpio.h>
Heikki Krogerusec464752010-08-19 15:09:36 +030034
35#include <linux/usb/otg.h>
36#include <linux/usb/ulpi.h>
37#include <linux/usb/ch9.h>
38#include <linux/usb/gadget.h>
Kalle Jokiniemi2785cef2011-03-29 16:27:59 +030039#include <linux/power/isp1704_charger.h>
Heikki Krogerusec464752010-08-19 15:09:36 +030040
41/* Vendor specific Power Control register */
42#define ISP1704_PWR_CTRL 0x3d
43#define ISP1704_PWR_CTRL_SWCTRL (1 << 0)
44#define ISP1704_PWR_CTRL_DET_COMP (1 << 1)
45#define ISP1704_PWR_CTRL_BVALID_RISE (1 << 2)
46#define ISP1704_PWR_CTRL_BVALID_FALL (1 << 3)
47#define ISP1704_PWR_CTRL_DP_WKPU_EN (1 << 4)
48#define ISP1704_PWR_CTRL_VDAT_DET (1 << 5)
49#define ISP1704_PWR_CTRL_DPVSRC_EN (1 << 6)
50#define ISP1704_PWR_CTRL_HWDETECT (1 << 7)
51
52#define NXP_VENDOR_ID 0x04cc
53
54static u16 isp170x_id[] = {
55 0x1704,
56 0x1707,
57};
58
59struct isp1704_charger {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010060 struct device *dev;
61 struct power_supply *psy;
62 struct power_supply_desc psy_desc;
63 struct usb_phy *phy;
64 struct notifier_block nb;
65 struct work_struct work;
Heikki Krogerusec464752010-08-19 15:09:36 +030066
Heikki Krogerusbac43b22010-11-04 16:31:47 +020067 /* properties */
Ameya Palande746d8fb2010-11-04 16:31:46 +020068 char model[8];
Heikki Krogerusec464752010-08-19 15:09:36 +030069 unsigned present:1;
Heikki Krogerusbac43b22010-11-04 16:31:47 +020070 unsigned online:1;
71 unsigned current_max;
Heikki Krogerusec464752010-08-19 15:09:36 +030072};
73
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +020074static inline int isp1704_read(struct isp1704_charger *isp, u32 reg)
75{
76 return usb_phy_io_read(isp->phy, reg);
77}
78
79static inline int isp1704_write(struct isp1704_charger *isp, u32 val, u32 reg)
80{
81 return usb_phy_io_write(isp->phy, val, reg);
82}
83
Heikki Krogerusec464752010-08-19 15:09:36 +030084/*
Kalle Jokiniemi2785cef2011-03-29 16:27:59 +030085 * Disable/enable the power from the isp1704 if a function for it
86 * has been provided with platform data.
87 */
88static void isp1704_charger_set_power(struct isp1704_charger *isp, bool on)
89{
90 struct isp1704_charger_data *board = isp->dev->platform_data;
91
Felipe Contrerasc9345022012-01-06 06:00:48 +040092 if (board && board->set_power)
Kalle Jokiniemi2785cef2011-03-29 16:27:59 +030093 board->set_power(on);
Sebastian Reichel34a10962013-12-01 02:00:10 +010094 else if (board)
95 gpio_set_value(board->enable_gpio, on);
Kalle Jokiniemi2785cef2011-03-29 16:27:59 +030096}
97
98/*
Heikki Krogerusbac43b22010-11-04 16:31:47 +020099 * Determine is the charging port DCP (dedicated charger) or CDP (Host/HUB
100 * chargers).
101 *
102 * REVISIT: The method is defined in Battery Charging Specification and is
103 * applicable to any ULPI transceiver. Nothing isp170x specific here.
104 */
105static inline int isp1704_charger_type(struct isp1704_charger *isp)
106{
107 u8 reg;
108 u8 func_ctrl;
109 u8 otg_ctrl;
110 int type = POWER_SUPPLY_TYPE_USB_DCP;
111
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200112 func_ctrl = isp1704_read(isp, ULPI_FUNC_CTRL);
113 otg_ctrl = isp1704_read(isp, ULPI_OTG_CTRL);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200114
115 /* disable pulldowns */
116 reg = ULPI_OTG_CTRL_DM_PULLDOWN | ULPI_OTG_CTRL_DP_PULLDOWN;
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200117 isp1704_write(isp, ULPI_CLR(ULPI_OTG_CTRL), reg);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200118
119 /* full speed */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200120 isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200121 ULPI_FUNC_CTRL_XCVRSEL_MASK);
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200122 isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL),
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200123 ULPI_FUNC_CTRL_FULL_SPEED);
124
125 /* Enable strong pull-up on DP (1.5K) and reset */
126 reg = ULPI_FUNC_CTRL_TERMSELECT | ULPI_FUNC_CTRL_RESET;
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200127 isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL), reg);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200128 usleep_range(1000, 2000);
129
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200130 reg = isp1704_read(isp, ULPI_DEBUG);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200131 if ((reg & 3) != 3)
132 type = POWER_SUPPLY_TYPE_USB_CDP;
133
134 /* recover original state */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200135 isp1704_write(isp, ULPI_FUNC_CTRL, func_ctrl);
136 isp1704_write(isp, ULPI_OTG_CTRL, otg_ctrl);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200137
138 return type;
139}
140
141/*
Heikki Krogerusec464752010-08-19 15:09:36 +0300142 * ISP1704 detects PS/2 adapters as charger. To make sure the detected charger
143 * is actually a dedicated charger, the following steps need to be taken.
144 */
145static inline int isp1704_charger_verify(struct isp1704_charger *isp)
146{
147 int ret = 0;
148 u8 r;
149
150 /* Reset the transceiver */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200151 r = isp1704_read(isp, ULPI_FUNC_CTRL);
Heikki Krogerusec464752010-08-19 15:09:36 +0300152 r |= ULPI_FUNC_CTRL_RESET;
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200153 isp1704_write(isp, ULPI_FUNC_CTRL, r);
Heikki Krogerusec464752010-08-19 15:09:36 +0300154 usleep_range(1000, 2000);
155
156 /* Set normal mode */
157 r &= ~(ULPI_FUNC_CTRL_RESET | ULPI_FUNC_CTRL_OPMODE_MASK);
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200158 isp1704_write(isp, ULPI_FUNC_CTRL, r);
Heikki Krogerusec464752010-08-19 15:09:36 +0300159
160 /* Clear the DP and DM pull-down bits */
161 r = ULPI_OTG_CTRL_DP_PULLDOWN | ULPI_OTG_CTRL_DM_PULLDOWN;
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200162 isp1704_write(isp, ULPI_CLR(ULPI_OTG_CTRL), r);
Heikki Krogerusec464752010-08-19 15:09:36 +0300163
164 /* Enable strong pull-up on DP (1.5K) and reset */
165 r = ULPI_FUNC_CTRL_TERMSELECT | ULPI_FUNC_CTRL_RESET;
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200166 isp1704_write(isp, ULPI_SET(ULPI_FUNC_CTRL), r);
Heikki Krogerusec464752010-08-19 15:09:36 +0300167 usleep_range(1000, 2000);
168
169 /* Read the line state */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200170 if (!isp1704_read(isp, ULPI_DEBUG)) {
Heikki Krogerusec464752010-08-19 15:09:36 +0300171 /* Disable strong pull-up on DP (1.5K) */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200172 isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
Heikki Krogerusec464752010-08-19 15:09:36 +0300173 ULPI_FUNC_CTRL_TERMSELECT);
174 return 1;
175 }
176
177 /* Is it a charger or PS/2 connection */
178
179 /* Enable weak pull-up resistor on DP */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200180 isp1704_write(isp, ULPI_SET(ISP1704_PWR_CTRL),
Heikki Krogerusec464752010-08-19 15:09:36 +0300181 ISP1704_PWR_CTRL_DP_WKPU_EN);
182
183 /* Disable strong pull-up on DP (1.5K) */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200184 isp1704_write(isp, ULPI_CLR(ULPI_FUNC_CTRL),
Heikki Krogerusec464752010-08-19 15:09:36 +0300185 ULPI_FUNC_CTRL_TERMSELECT);
186
187 /* Enable weak pull-down resistor on DM */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200188 isp1704_write(isp, ULPI_SET(ULPI_OTG_CTRL),
Heikki Krogerusec464752010-08-19 15:09:36 +0300189 ULPI_OTG_CTRL_DM_PULLDOWN);
190
191 /* It's a charger if the line states are clear */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200192 if (!(isp1704_read(isp, ULPI_DEBUG)))
Heikki Krogerusec464752010-08-19 15:09:36 +0300193 ret = 1;
194
195 /* Disable weak pull-up resistor on DP */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200196 isp1704_write(isp, ULPI_CLR(ISP1704_PWR_CTRL),
Heikki Krogerusec464752010-08-19 15:09:36 +0300197 ISP1704_PWR_CTRL_DP_WKPU_EN);
198
199 return ret;
200}
201
202static inline int isp1704_charger_detect(struct isp1704_charger *isp)
203{
204 unsigned long timeout;
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200205 u8 pwr_ctrl;
Heikki Krogerusec464752010-08-19 15:09:36 +0300206 int ret = 0;
207
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200208 pwr_ctrl = isp1704_read(isp, ISP1704_PWR_CTRL);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200209
Heikki Krogerusec464752010-08-19 15:09:36 +0300210 /* set SW control bit in PWR_CTRL register */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200211 isp1704_write(isp, ISP1704_PWR_CTRL,
Heikki Krogerusec464752010-08-19 15:09:36 +0300212 ISP1704_PWR_CTRL_SWCTRL);
213
214 /* enable manual charger detection */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200215 isp1704_write(isp, ULPI_SET(ISP1704_PWR_CTRL),
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200216 ISP1704_PWR_CTRL_SWCTRL
217 | ISP1704_PWR_CTRL_DPVSRC_EN);
Heikki Krogerusec464752010-08-19 15:09:36 +0300218 usleep_range(1000, 2000);
219
220 timeout = jiffies + msecs_to_jiffies(300);
221 do {
222 /* Check if there is a charger */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200223 if (isp1704_read(isp, ISP1704_PWR_CTRL)
Heikki Krogerusec464752010-08-19 15:09:36 +0300224 & ISP1704_PWR_CTRL_VDAT_DET) {
225 ret = isp1704_charger_verify(isp);
226 break;
227 }
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200228 } while (!time_after(jiffies, timeout) && isp->online);
229
230 /* recover original state */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200231 isp1704_write(isp, ISP1704_PWR_CTRL, pwr_ctrl);
Heikki Krogerusec464752010-08-19 15:09:36 +0300232
233 return ret;
234}
235
Pali Rohárf07c11e2013-09-08 10:50:37 +0200236static inline int isp1704_charger_detect_dcp(struct isp1704_charger *isp)
237{
238 if (isp1704_charger_detect(isp) &&
239 isp1704_charger_type(isp) == POWER_SUPPLY_TYPE_USB_DCP)
240 return true;
241 else
242 return false;
243}
244
Heikki Krogerusec464752010-08-19 15:09:36 +0300245static void isp1704_charger_work(struct work_struct *data)
246{
Heikki Krogerusec464752010-08-19 15:09:36 +0300247 struct isp1704_charger *isp =
248 container_of(data, struct isp1704_charger, work);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200249 static DEFINE_MUTEX(lock);
Heikki Krogerusec464752010-08-19 15:09:36 +0300250
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200251 mutex_lock(&lock);
Heikki Krogerusec464752010-08-19 15:09:36 +0300252
Pali Rohárf07c11e2013-09-08 10:50:37 +0200253 switch (isp->phy->last_event) {
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200254 case USB_EVENT_VBUS:
Pali Rohárf07c11e2013-09-08 10:50:37 +0200255 /* do not call wall charger detection more times */
256 if (!isp->present) {
257 isp->online = true;
258 isp->present = 1;
259 isp1704_charger_set_power(isp, 1);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200260
Pali Rohárf07c11e2013-09-08 10:50:37 +0200261 /* detect wall charger */
262 if (isp1704_charger_detect_dcp(isp)) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100263 isp->psy_desc.type = POWER_SUPPLY_TYPE_USB_DCP;
Pali Rohárf07c11e2013-09-08 10:50:37 +0200264 isp->current_max = 1800;
265 } else {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100266 isp->psy_desc.type = POWER_SUPPLY_TYPE_USB;
Pali Rohárf07c11e2013-09-08 10:50:37 +0200267 isp->current_max = 500;
268 }
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200269
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200270 /* enable data pullups */
Heikki Krogerusb1c711d2012-02-13 13:24:17 +0200271 if (isp->phy->otg->gadget)
272 usb_gadget_connect(isp->phy->otg->gadget);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200273 }
Pali Rohárf07c11e2013-09-08 10:50:37 +0200274
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100275 if (isp->psy_desc.type != POWER_SUPPLY_TYPE_USB_DCP) {
Pali Rohárf07c11e2013-09-08 10:50:37 +0200276 /*
277 * Only 500mA here or high speed chirp
278 * handshaking may break
279 */
280 if (isp->current_max > 500)
281 isp->current_max = 500;
282
283 if (isp->current_max > 100)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100284 isp->psy_desc.type = POWER_SUPPLY_TYPE_USB_CDP;
Pali Rohárf07c11e2013-09-08 10:50:37 +0200285 }
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200286 break;
287 case USB_EVENT_NONE:
288 isp->online = false;
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200289 isp->present = 0;
290 isp->current_max = 0;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100291 isp->psy_desc.type = POWER_SUPPLY_TYPE_USB;
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200292
293 /*
294 * Disable data pullups. We need to prevent the controller from
295 * enumerating.
296 *
297 * FIXME: This is here to allow charger detection with Host/HUB
298 * chargers. The pullups may be enabled elsewhere, so this can
299 * not be the final solution.
300 */
Heikki Krogerusb1c711d2012-02-13 13:24:17 +0200301 if (isp->phy->otg->gadget)
302 usb_gadget_disconnect(isp->phy->otg->gadget);
Kalle Jokiniemi2785cef2011-03-29 16:27:59 +0300303
304 isp1704_charger_set_power(isp, 0);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200305 break;
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200306 default:
307 goto out;
Heikki Krogerusec464752010-08-19 15:09:36 +0300308 }
309
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100310 power_supply_changed(isp->psy);
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200311out:
312 mutex_unlock(&lock);
Heikki Krogerusec464752010-08-19 15:09:36 +0300313}
314
315static int isp1704_notifier_call(struct notifier_block *nb,
Pali Rohárf07c11e2013-09-08 10:50:37 +0200316 unsigned long val, void *v)
Heikki Krogerusec464752010-08-19 15:09:36 +0300317{
318 struct isp1704_charger *isp =
319 container_of(nb, struct isp1704_charger, nb);
320
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200321 schedule_work(&isp->work);
Heikki Krogerusec464752010-08-19 15:09:36 +0300322
323 return NOTIFY_OK;
324}
325
326static int isp1704_charger_get_property(struct power_supply *psy,
327 enum power_supply_property psp,
328 union power_supply_propval *val)
329{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100330 struct isp1704_charger *isp = power_supply_get_drvdata(psy);
Heikki Krogerusec464752010-08-19 15:09:36 +0300331
332 switch (psp) {
333 case POWER_SUPPLY_PROP_PRESENT:
334 val->intval = isp->present;
335 break;
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200336 case POWER_SUPPLY_PROP_ONLINE:
337 val->intval = isp->online;
338 break;
339 case POWER_SUPPLY_PROP_CURRENT_MAX:
340 val->intval = isp->current_max;
341 break;
Heikki Krogerusec464752010-08-19 15:09:36 +0300342 case POWER_SUPPLY_PROP_MODEL_NAME:
343 val->strval = isp->model;
344 break;
345 case POWER_SUPPLY_PROP_MANUFACTURER:
346 val->strval = "NXP";
347 break;
348 default:
349 return -EINVAL;
350 }
351 return 0;
352}
353
354static enum power_supply_property power_props[] = {
355 POWER_SUPPLY_PROP_PRESENT,
Heikki Krogerusbac43b22010-11-04 16:31:47 +0200356 POWER_SUPPLY_PROP_ONLINE,
357 POWER_SUPPLY_PROP_CURRENT_MAX,
Heikki Krogerusec464752010-08-19 15:09:36 +0300358 POWER_SUPPLY_PROP_MODEL_NAME,
359 POWER_SUPPLY_PROP_MANUFACTURER,
360};
361
362static inline int isp1704_test_ulpi(struct isp1704_charger *isp)
363{
364 int vendor;
365 int product;
366 int i;
367 int ret = -ENODEV;
368
369 /* Test ULPI interface */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200370 ret = isp1704_write(isp, ULPI_SCRATCH, 0xaa);
Heikki Krogerusec464752010-08-19 15:09:36 +0300371 if (ret < 0)
372 return ret;
373
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200374 ret = isp1704_read(isp, ULPI_SCRATCH);
Heikki Krogerusec464752010-08-19 15:09:36 +0300375 if (ret < 0)
376 return ret;
377
378 if (ret != 0xaa)
379 return -ENODEV;
380
381 /* Verify the product and vendor id matches */
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200382 vendor = isp1704_read(isp, ULPI_VENDOR_ID_LOW);
383 vendor |= isp1704_read(isp, ULPI_VENDOR_ID_HIGH) << 8;
Heikki Krogerusec464752010-08-19 15:09:36 +0300384 if (vendor != NXP_VENDOR_ID)
385 return -ENODEV;
386
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200387 product = isp1704_read(isp, ULPI_PRODUCT_ID_LOW);
388 product |= isp1704_read(isp, ULPI_PRODUCT_ID_HIGH) << 8;
Heikki Krogerusec464752010-08-19 15:09:36 +0300389
390 for (i = 0; i < ARRAY_SIZE(isp170x_id); i++) {
391 if (product == isp170x_id[i]) {
392 sprintf(isp->model, "isp%x", product);
393 return product;
394 }
395 }
396
397 dev_err(isp->dev, "product id %x not matching known ids", product);
398
399 return -ENODEV;
400}
401
Bill Pembertonc8afa642012-11-19 13:22:23 -0500402static int isp1704_charger_probe(struct platform_device *pdev)
Heikki Krogerusec464752010-08-19 15:09:36 +0300403{
404 struct isp1704_charger *isp;
405 int ret = -ENODEV;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100406 struct power_supply_config psy_cfg = {};
Heikki Krogerusec464752010-08-19 15:09:36 +0300407
Sebastian Reichel34a10962013-12-01 02:00:10 +0100408 struct isp1704_charger_data *pdata = dev_get_platdata(&pdev->dev);
409 struct device_node *np = pdev->dev.of_node;
410
411 if (np) {
412 int gpio = of_get_named_gpio(np, "nxp,enable-gpio", 0);
413
414 if (gpio < 0)
415 return gpio;
416
417 pdata = devm_kzalloc(&pdev->dev,
418 sizeof(struct isp1704_charger_data), GFP_KERNEL);
419 pdata->enable_gpio = gpio;
420
421 dev_info(&pdev->dev, "init gpio %d\n", pdata->enable_gpio);
422
423 ret = devm_gpio_request_one(&pdev->dev, pdata->enable_gpio,
424 GPIOF_OUT_INIT_HIGH, "isp1704_reset");
425 if (ret)
426 goto fail0;
427 }
428
429 if (!pdata) {
430 dev_err(&pdev->dev, "missing platform data!\n");
431 return -ENODEV;
432 }
433
434
Jingoo Han2a2ce522013-03-11 15:34:15 +0900435 isp = devm_kzalloc(&pdev->dev, sizeof(*isp), GFP_KERNEL);
Heikki Krogerusec464752010-08-19 15:09:36 +0300436 if (!isp)
437 return -ENOMEM;
438
Sebastian Reichel34a10962013-12-01 02:00:10 +0100439 if (np)
440 isp->phy = devm_usb_get_phy_by_phandle(&pdev->dev, "usb-phy", 0);
441 else
442 isp->phy = devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
443
444 if (IS_ERR(isp->phy)) {
445 ret = PTR_ERR(isp->phy);
446 goto fail0;
447 }
Heikki Krogerusec464752010-08-19 15:09:36 +0300448
Heikki Krogerusa4607d92010-11-04 16:31:48 +0200449 isp->dev = &pdev->dev;
450 platform_set_drvdata(pdev, isp);
451
Kalle Jokiniemi2785cef2011-03-29 16:27:59 +0300452 isp1704_charger_set_power(isp, 1);
453
Heikki Krogerusec464752010-08-19 15:09:36 +0300454 ret = isp1704_test_ulpi(isp);
455 if (ret < 0)
456 goto fail1;
457
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100458 isp->psy_desc.name = "isp1704";
459 isp->psy_desc.type = POWER_SUPPLY_TYPE_USB;
460 isp->psy_desc.properties = power_props;
461 isp->psy_desc.num_properties = ARRAY_SIZE(power_props);
462 isp->psy_desc.get_property = isp1704_charger_get_property;
Heikki Krogerusec464752010-08-19 15:09:36 +0300463
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100464 psy_cfg.drv_data = isp;
465
466 isp->psy = power_supply_register(isp->dev, &isp->psy_desc, &psy_cfg);
467 if (IS_ERR(isp->psy)) {
468 ret = PTR_ERR(isp->psy);
Heikki Krogerusec464752010-08-19 15:09:36 +0300469 goto fail1;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100470 }
Heikki Krogerusec464752010-08-19 15:09:36 +0300471
472 /*
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200473 * REVISIT: using work in order to allow the usb notifications to be
Heikki Krogerusec464752010-08-19 15:09:36 +0300474 * made atomically in the future.
475 */
476 INIT_WORK(&isp->work, isp1704_charger_work);
477
478 isp->nb.notifier_call = isp1704_notifier_call;
479
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200480 ret = usb_register_notifier(isp->phy, &isp->nb);
Heikki Krogerusec464752010-08-19 15:09:36 +0300481 if (ret)
482 goto fail2;
483
484 dev_info(isp->dev, "registered with product id %s\n", isp->model);
485
Heikki Krogeruse1a85694e2010-11-08 12:22:40 +0200486 /*
487 * Taking over the D+ pullup.
488 *
489 * FIXME: The device will be disconnected if it was already
490 * enumerated. The charger driver should be always loaded before any
491 * gadget is loaded.
492 */
Heikki Krogerusb1c711d2012-02-13 13:24:17 +0200493 if (isp->phy->otg->gadget)
494 usb_gadget_disconnect(isp->phy->otg->gadget);
Heikki Krogeruse1a85694e2010-11-08 12:22:40 +0200495
Pali Rohárf07c11e2013-09-08 10:50:37 +0200496 if (isp->phy->last_event == USB_EVENT_NONE)
497 isp1704_charger_set_power(isp, 0);
498
Heikki Krogeruse1a85694e2010-11-08 12:22:40 +0200499 /* Detect charger if VBUS is valid (the cable was already plugged). */
Pali Rohárf07c11e2013-09-08 10:50:37 +0200500 if (isp->phy->last_event == USB_EVENT_VBUS &&
501 !isp->phy->otg->default_a)
Heikki Krogeruse1a85694e2010-11-08 12:22:40 +0200502 schedule_work(&isp->work);
Heikki Krogeruse1a85694e2010-11-08 12:22:40 +0200503
Heikki Krogerusec464752010-08-19 15:09:36 +0300504 return 0;
505fail2:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100506 power_supply_unregister(isp->psy);
Heikki Krogerusec464752010-08-19 15:09:36 +0300507fail1:
Dan Carpenter81a08382012-03-27 23:22:13 +0300508 isp1704_charger_set_power(isp, 0);
Heikki Krogerusec464752010-08-19 15:09:36 +0300509fail0:
Heikki Krogerusec464752010-08-19 15:09:36 +0300510 dev_err(&pdev->dev, "failed to register isp1704 with error %d\n", ret);
511
512 return ret;
513}
514
Bill Pemberton415ec692012-11-19 13:26:07 -0500515static int isp1704_charger_remove(struct platform_device *pdev)
Heikki Krogerusec464752010-08-19 15:09:36 +0300516{
517 struct isp1704_charger *isp = platform_get_drvdata(pdev);
518
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200519 usb_unregister_notifier(isp->phy, &isp->nb);
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100520 power_supply_unregister(isp->psy);
Kalle Jokiniemi2785cef2011-03-29 16:27:59 +0300521 isp1704_charger_set_power(isp, 0);
Heikki Krogerusec464752010-08-19 15:09:36 +0300522
523 return 0;
524}
525
Sebastian Reichel34a10962013-12-01 02:00:10 +0100526#ifdef CONFIG_OF
527static const struct of_device_id omap_isp1704_of_match[] = {
528 { .compatible = "nxp,isp1704", },
529 {},
530};
531MODULE_DEVICE_TABLE(of, omap_isp1704_of_match);
532#endif
533
Heikki Krogerusec464752010-08-19 15:09:36 +0300534static struct platform_driver isp1704_charger_driver = {
535 .driver = {
536 .name = "isp1704_charger",
Sebastian Reichel34a10962013-12-01 02:00:10 +0100537 .of_match_table = of_match_ptr(omap_isp1704_of_match),
Heikki Krogerusec464752010-08-19 15:09:36 +0300538 },
539 .probe = isp1704_charger_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500540 .remove = isp1704_charger_remove,
Heikki Krogerusec464752010-08-19 15:09:36 +0300541};
542
Axel Lin300bac72011-11-26 12:01:10 +0800543module_platform_driver(isp1704_charger_driver);
Heikki Krogerusec464752010-08-19 15:09:36 +0300544
545MODULE_ALIAS("platform:isp1704_charger");
546MODULE_AUTHOR("Nokia Corporation");
547MODULE_DESCRIPTION("ISP170x USB Charger driver");
548MODULE_LICENSE("GPL");