ARM: dts: add 'dr_mode' property to hsotg devices for exynos boards
[linux/fpc-iii.git] / drivers / mfd / rtsx_usb.c
blobdbdd0faeb6ce500678d9dc4f014504560693bded
1 /* Driver for Realtek USB card reader
3 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, see <http://www.gnu.org/licenses/>.
17 * Author:
18 * Roger Tseng <rogerable@realtek.com>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/mutex.h>
23 #include <linux/usb.h>
24 #include <linux/platform_device.h>
25 #include <linux/mfd/core.h>
26 #include <linux/mfd/rtsx_usb.h>
28 static int polling_pipe = 1;
29 module_param(polling_pipe, int, S_IRUGO | S_IWUSR);
30 MODULE_PARM_DESC(polling_pipe, "polling pipe (0: ctl, 1: bulk)");
32 static const struct mfd_cell rtsx_usb_cells[] = {
33 [RTSX_USB_SD_CARD] = {
34 .name = "rtsx_usb_sdmmc",
35 .pdata_size = 0,
37 [RTSX_USB_MS_CARD] = {
38 .name = "rtsx_usb_ms",
39 .pdata_size = 0,
43 static void rtsx_usb_sg_timed_out(unsigned long data)
45 struct rtsx_ucr *ucr = (struct rtsx_ucr *)data;
47 dev_dbg(&ucr->pusb_intf->dev, "%s: sg transfer timed out", __func__);
48 usb_sg_cancel(&ucr->current_sg);
50 /* we know the cancellation is caused by time-out */
51 ucr->current_sg.status = -ETIMEDOUT;
54 static int rtsx_usb_bulk_transfer_sglist(struct rtsx_ucr *ucr,
55 unsigned int pipe, struct scatterlist *sg, int num_sg,
56 unsigned int length, unsigned int *act_len, int timeout)
58 int ret;
60 dev_dbg(&ucr->pusb_intf->dev, "%s: xfer %u bytes, %d entries\n",
61 __func__, length, num_sg);
62 ret = usb_sg_init(&ucr->current_sg, ucr->pusb_dev, pipe, 0,
63 sg, num_sg, length, GFP_NOIO);
64 if (ret)
65 return ret;
67 ucr->sg_timer.expires = jiffies + msecs_to_jiffies(timeout);
68 add_timer(&ucr->sg_timer);
69 usb_sg_wait(&ucr->current_sg);
70 del_timer_sync(&ucr->sg_timer);
72 if (act_len)
73 *act_len = ucr->current_sg.bytes;
75 return ucr->current_sg.status;
78 int rtsx_usb_transfer_data(struct rtsx_ucr *ucr, unsigned int pipe,
79 void *buf, unsigned int len, int num_sg,
80 unsigned int *act_len, int timeout)
82 if (timeout < 600)
83 timeout = 600;
85 if (num_sg)
86 return rtsx_usb_bulk_transfer_sglist(ucr, pipe,
87 (struct scatterlist *)buf, num_sg, len, act_len,
88 timeout);
89 else
90 return usb_bulk_msg(ucr->pusb_dev, pipe, buf, len, act_len,
91 timeout);
93 EXPORT_SYMBOL_GPL(rtsx_usb_transfer_data);
95 static inline void rtsx_usb_seq_cmd_hdr(struct rtsx_ucr *ucr,
96 u16 addr, u16 len, u8 seq_type)
98 rtsx_usb_cmd_hdr_tag(ucr);
100 ucr->cmd_buf[PACKET_TYPE] = seq_type;
101 ucr->cmd_buf[5] = (u8)(len >> 8);
102 ucr->cmd_buf[6] = (u8)len;
103 ucr->cmd_buf[8] = (u8)(addr >> 8);
104 ucr->cmd_buf[9] = (u8)addr;
106 if (seq_type == SEQ_WRITE)
107 ucr->cmd_buf[STAGE_FLAG] = 0;
108 else
109 ucr->cmd_buf[STAGE_FLAG] = STAGE_R;
112 static int rtsx_usb_seq_write_register(struct rtsx_ucr *ucr,
113 u16 addr, u16 len, u8 *data)
115 u16 cmd_len = ALIGN(SEQ_WRITE_DATA_OFFSET + len, 4);
117 if (!data)
118 return -EINVAL;
120 if (cmd_len > IOBUF_SIZE)
121 return -EINVAL;
123 rtsx_usb_seq_cmd_hdr(ucr, addr, len, SEQ_WRITE);
124 memcpy(ucr->cmd_buf + SEQ_WRITE_DATA_OFFSET, data, len);
126 return rtsx_usb_transfer_data(ucr,
127 usb_sndbulkpipe(ucr->pusb_dev, EP_BULK_OUT),
128 ucr->cmd_buf, cmd_len, 0, NULL, 100);
131 static int rtsx_usb_seq_read_register(struct rtsx_ucr *ucr,
132 u16 addr, u16 len, u8 *data)
134 int i, ret;
135 u16 rsp_len = round_down(len, 4);
136 u16 res_len = len - rsp_len;
138 if (!data)
139 return -EINVAL;
141 /* 4-byte aligned part */
142 if (rsp_len) {
143 rtsx_usb_seq_cmd_hdr(ucr, addr, len, SEQ_READ);
144 ret = rtsx_usb_transfer_data(ucr,
145 usb_sndbulkpipe(ucr->pusb_dev, EP_BULK_OUT),
146 ucr->cmd_buf, 12, 0, NULL, 100);
147 if (ret)
148 return ret;
150 ret = rtsx_usb_transfer_data(ucr,
151 usb_rcvbulkpipe(ucr->pusb_dev, EP_BULK_IN),
152 data, rsp_len, 0, NULL, 100);
153 if (ret)
154 return ret;
157 /* unaligned part */
158 for (i = 0; i < res_len; i++) {
159 ret = rtsx_usb_read_register(ucr, addr + rsp_len + i,
160 data + rsp_len + i);
161 if (ret)
162 return ret;
165 return 0;
168 int rtsx_usb_read_ppbuf(struct rtsx_ucr *ucr, u8 *buf, int buf_len)
170 return rtsx_usb_seq_read_register(ucr, PPBUF_BASE2, (u16)buf_len, buf);
172 EXPORT_SYMBOL_GPL(rtsx_usb_read_ppbuf);
174 int rtsx_usb_write_ppbuf(struct rtsx_ucr *ucr, u8 *buf, int buf_len)
176 return rtsx_usb_seq_write_register(ucr, PPBUF_BASE2, (u16)buf_len, buf);
178 EXPORT_SYMBOL_GPL(rtsx_usb_write_ppbuf);
180 int rtsx_usb_ep0_write_register(struct rtsx_ucr *ucr, u16 addr,
181 u8 mask, u8 data)
183 u16 value, index;
185 addr |= EP0_WRITE_REG_CMD << EP0_OP_SHIFT;
186 value = swab16(addr);
187 index = mask | data << 8;
189 return usb_control_msg(ucr->pusb_dev,
190 usb_sndctrlpipe(ucr->pusb_dev, 0), RTSX_USB_REQ_REG_OP,
191 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
192 value, index, NULL, 0, 100);
194 EXPORT_SYMBOL_GPL(rtsx_usb_ep0_write_register);
196 int rtsx_usb_ep0_read_register(struct rtsx_ucr *ucr, u16 addr, u8 *data)
198 u16 value;
200 if (!data)
201 return -EINVAL;
202 *data = 0;
204 addr |= EP0_READ_REG_CMD << EP0_OP_SHIFT;
205 value = swab16(addr);
207 return usb_control_msg(ucr->pusb_dev,
208 usb_rcvctrlpipe(ucr->pusb_dev, 0), RTSX_USB_REQ_REG_OP,
209 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
210 value, 0, data, 1, 100);
212 EXPORT_SYMBOL_GPL(rtsx_usb_ep0_read_register);
214 void rtsx_usb_add_cmd(struct rtsx_ucr *ucr, u8 cmd_type, u16 reg_addr,
215 u8 mask, u8 data)
217 int i;
219 if (ucr->cmd_idx < (IOBUF_SIZE - CMD_OFFSET) / 4) {
220 i = CMD_OFFSET + ucr->cmd_idx * 4;
222 ucr->cmd_buf[i++] = ((cmd_type & 0x03) << 6) |
223 (u8)((reg_addr >> 8) & 0x3F);
224 ucr->cmd_buf[i++] = (u8)reg_addr;
225 ucr->cmd_buf[i++] = mask;
226 ucr->cmd_buf[i++] = data;
228 ucr->cmd_idx++;
231 EXPORT_SYMBOL_GPL(rtsx_usb_add_cmd);
233 int rtsx_usb_send_cmd(struct rtsx_ucr *ucr, u8 flag, int timeout)
235 int ret;
237 ucr->cmd_buf[CNT_H] = (u8)(ucr->cmd_idx >> 8);
238 ucr->cmd_buf[CNT_L] = (u8)(ucr->cmd_idx);
239 ucr->cmd_buf[STAGE_FLAG] = flag;
241 ret = rtsx_usb_transfer_data(ucr,
242 usb_sndbulkpipe(ucr->pusb_dev, EP_BULK_OUT),
243 ucr->cmd_buf, ucr->cmd_idx * 4 + CMD_OFFSET,
244 0, NULL, timeout);
245 if (ret) {
246 rtsx_usb_clear_fsm_err(ucr);
247 return ret;
250 return 0;
252 EXPORT_SYMBOL_GPL(rtsx_usb_send_cmd);
254 int rtsx_usb_get_rsp(struct rtsx_ucr *ucr, int rsp_len, int timeout)
256 if (rsp_len <= 0)
257 return -EINVAL;
259 rsp_len = ALIGN(rsp_len, 4);
261 return rtsx_usb_transfer_data(ucr,
262 usb_rcvbulkpipe(ucr->pusb_dev, EP_BULK_IN),
263 ucr->rsp_buf, rsp_len, 0, NULL, timeout);
265 EXPORT_SYMBOL_GPL(rtsx_usb_get_rsp);
267 static int rtsx_usb_get_status_with_bulk(struct rtsx_ucr *ucr, u16 *status)
269 int ret;
271 rtsx_usb_init_cmd(ucr);
272 rtsx_usb_add_cmd(ucr, READ_REG_CMD, CARD_EXIST, 0x00, 0x00);
273 rtsx_usb_add_cmd(ucr, READ_REG_CMD, OCPSTAT, 0x00, 0x00);
274 ret = rtsx_usb_send_cmd(ucr, MODE_CR, 100);
275 if (ret)
276 return ret;
278 ret = rtsx_usb_get_rsp(ucr, 2, 100);
279 if (ret)
280 return ret;
282 *status = ((ucr->rsp_buf[0] >> 2) & 0x0f) |
283 ((ucr->rsp_buf[1] & 0x03) << 4);
285 return 0;
288 int rtsx_usb_get_card_status(struct rtsx_ucr *ucr, u16 *status)
290 int ret;
292 if (!status)
293 return -EINVAL;
295 if (polling_pipe == 0)
296 ret = usb_control_msg(ucr->pusb_dev,
297 usb_rcvctrlpipe(ucr->pusb_dev, 0),
298 RTSX_USB_REQ_POLL,
299 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
300 0, 0, status, 2, 100);
301 else
302 ret = rtsx_usb_get_status_with_bulk(ucr, status);
304 /* usb_control_msg may return positive when success */
305 if (ret < 0)
306 return ret;
308 return 0;
310 EXPORT_SYMBOL_GPL(rtsx_usb_get_card_status);
312 static int rtsx_usb_write_phy_register(struct rtsx_ucr *ucr, u8 addr, u8 val)
314 dev_dbg(&ucr->pusb_intf->dev, "Write 0x%x to phy register 0x%x\n",
315 val, addr);
317 rtsx_usb_init_cmd(ucr);
319 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VSTAIN, 0xFF, val);
320 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VCONTROL, 0xFF, addr & 0x0F);
321 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00);
322 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00);
323 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x01);
324 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VCONTROL,
325 0xFF, (addr >> 4) & 0x0F);
326 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00);
327 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00);
328 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x01);
330 return rtsx_usb_send_cmd(ucr, MODE_C, 100);
333 int rtsx_usb_write_register(struct rtsx_ucr *ucr, u16 addr, u8 mask, u8 data)
335 rtsx_usb_init_cmd(ucr);
336 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, addr, mask, data);
337 return rtsx_usb_send_cmd(ucr, MODE_C, 100);
339 EXPORT_SYMBOL_GPL(rtsx_usb_write_register);
341 int rtsx_usb_read_register(struct rtsx_ucr *ucr, u16 addr, u8 *data)
343 int ret;
345 if (data != NULL)
346 *data = 0;
348 rtsx_usb_init_cmd(ucr);
349 rtsx_usb_add_cmd(ucr, READ_REG_CMD, addr, 0, 0);
350 ret = rtsx_usb_send_cmd(ucr, MODE_CR, 100);
351 if (ret)
352 return ret;
354 ret = rtsx_usb_get_rsp(ucr, 1, 100);
355 if (ret)
356 return ret;
358 if (data != NULL)
359 *data = ucr->rsp_buf[0];
361 return 0;
363 EXPORT_SYMBOL_GPL(rtsx_usb_read_register);
365 static inline u8 double_ssc_depth(u8 depth)
367 return (depth > 1) ? (depth - 1) : depth;
370 static u8 revise_ssc_depth(u8 ssc_depth, u8 div)
372 if (div > CLK_DIV_1) {
373 if (ssc_depth > div - 1)
374 ssc_depth -= (div - 1);
375 else
376 ssc_depth = SSC_DEPTH_2M;
379 return ssc_depth;
382 int rtsx_usb_switch_clock(struct rtsx_ucr *ucr, unsigned int card_clock,
383 u8 ssc_depth, bool initial_mode, bool double_clk, bool vpclk)
385 int ret;
386 u8 n, clk_divider, mcu_cnt, div;
388 if (!card_clock) {
389 ucr->cur_clk = 0;
390 return 0;
393 if (initial_mode) {
394 /* We use 250k(around) here, in initial stage */
395 clk_divider = SD_CLK_DIVIDE_128;
396 card_clock = 30000000;
397 } else {
398 clk_divider = SD_CLK_DIVIDE_0;
401 ret = rtsx_usb_write_register(ucr, SD_CFG1,
402 SD_CLK_DIVIDE_MASK, clk_divider);
403 if (ret < 0)
404 return ret;
406 card_clock /= 1000000;
407 dev_dbg(&ucr->pusb_intf->dev,
408 "Switch card clock to %dMHz\n", card_clock);
410 if (!initial_mode && double_clk)
411 card_clock *= 2;
412 dev_dbg(&ucr->pusb_intf->dev,
413 "Internal SSC clock: %dMHz (cur_clk = %d)\n",
414 card_clock, ucr->cur_clk);
416 if (card_clock == ucr->cur_clk)
417 return 0;
419 /* Converting clock value into internal settings: n and div */
420 n = card_clock - 2;
421 if ((card_clock <= 2) || (n > MAX_DIV_N))
422 return -EINVAL;
424 mcu_cnt = 60/card_clock + 3;
425 if (mcu_cnt > 15)
426 mcu_cnt = 15;
428 /* Make sure that the SSC clock div_n is not less than MIN_DIV_N */
430 div = CLK_DIV_1;
431 while (n < MIN_DIV_N && div < CLK_DIV_4) {
432 n = (n + 2) * 2 - 2;
433 div++;
435 dev_dbg(&ucr->pusb_intf->dev, "n = %d, div = %d\n", n, div);
437 if (double_clk)
438 ssc_depth = double_ssc_depth(ssc_depth);
440 ssc_depth = revise_ssc_depth(ssc_depth, div);
441 dev_dbg(&ucr->pusb_intf->dev, "ssc_depth = %d\n", ssc_depth);
443 rtsx_usb_init_cmd(ucr);
444 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CLK_DIV, CLK_CHANGE, CLK_CHANGE);
445 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CLK_DIV,
446 0x3F, (div << 4) | mcu_cnt);
447 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, 0);
448 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_CTL2,
449 SSC_DEPTH_MASK, ssc_depth);
450 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_DIV_N_0, 0xFF, n);
451 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, SSC_RSTB);
452 if (vpclk) {
453 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SD_VPCLK0_CTL,
454 PHASE_NOT_RESET, 0);
455 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SD_VPCLK0_CTL,
456 PHASE_NOT_RESET, PHASE_NOT_RESET);
459 ret = rtsx_usb_send_cmd(ucr, MODE_C, 2000);
460 if (ret < 0)
461 return ret;
463 ret = rtsx_usb_write_register(ucr, SSC_CTL1, 0xff,
464 SSC_RSTB | SSC_8X_EN | SSC_SEL_4M);
465 if (ret < 0)
466 return ret;
468 /* Wait SSC clock stable */
469 usleep_range(100, 1000);
471 ret = rtsx_usb_write_register(ucr, CLK_DIV, CLK_CHANGE, 0);
472 if (ret < 0)
473 return ret;
475 ucr->cur_clk = card_clock;
477 return 0;
479 EXPORT_SYMBOL_GPL(rtsx_usb_switch_clock);
481 int rtsx_usb_card_exclusive_check(struct rtsx_ucr *ucr, int card)
483 int ret;
484 u16 val;
485 u16 cd_mask[] = {
486 [RTSX_USB_SD_CARD] = (CD_MASK & ~SD_CD),
487 [RTSX_USB_MS_CARD] = (CD_MASK & ~MS_CD)
490 ret = rtsx_usb_get_card_status(ucr, &val);
492 * If get status fails, return 0 (ok) for the exclusive check
493 * and let the flow fail at somewhere else.
495 if (ret)
496 return 0;
498 if (val & cd_mask[card])
499 return -EIO;
501 return 0;
503 EXPORT_SYMBOL_GPL(rtsx_usb_card_exclusive_check);
505 static int rtsx_usb_reset_chip(struct rtsx_ucr *ucr)
507 int ret;
508 u8 val;
510 rtsx_usb_init_cmd(ucr);
512 if (CHECK_PKG(ucr, LQFP48)) {
513 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PWR_CTL,
514 LDO3318_PWR_MASK, LDO_SUSPEND);
515 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PWR_CTL,
516 FORCE_LDO_POWERB, FORCE_LDO_POWERB);
517 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL1,
518 0x30, 0x10);
519 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL5,
520 0x03, 0x01);
521 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL6,
522 0x0C, 0x04);
525 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SYS_DUMMY0, NYET_MSAK, NYET_EN);
526 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CD_DEGLITCH_WIDTH, 0xFF, 0x08);
527 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD,
528 CD_DEGLITCH_EN, XD_CD_DEGLITCH_EN, 0x0);
529 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SD30_DRIVE_SEL,
530 SD30_DRIVE_MASK, DRIVER_TYPE_D);
531 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD,
532 CARD_DRIVE_SEL, SD20_DRIVE_MASK, 0x0);
533 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, LDO_POWER_CFG, 0xE0, 0x0);
535 if (ucr->is_rts5179)
536 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD,
537 CARD_PULL_CTL5, 0x03, 0x01);
539 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_DMA1_CTL,
540 EXTEND_DMA1_ASYNC_SIGNAL, EXTEND_DMA1_ASYNC_SIGNAL);
541 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_INT_PEND,
542 XD_INT | MS_INT | SD_INT,
543 XD_INT | MS_INT | SD_INT);
545 ret = rtsx_usb_send_cmd(ucr, MODE_C, 100);
546 if (ret)
547 return ret;
549 /* config non-crystal mode */
550 rtsx_usb_read_register(ucr, CFG_MODE, &val);
551 if ((val & XTAL_FREE) || ((val & CLK_MODE_MASK) == CLK_MODE_NON_XTAL)) {
552 ret = rtsx_usb_write_phy_register(ucr, 0xC2, 0x7C);
553 if (ret)
554 return ret;
557 return 0;
560 static int rtsx_usb_init_chip(struct rtsx_ucr *ucr)
562 int ret;
563 u8 val;
565 rtsx_usb_clear_fsm_err(ucr);
567 /* power on SSC */
568 ret = rtsx_usb_write_register(ucr,
569 FPDCTL, SSC_POWER_MASK, SSC_POWER_ON);
570 if (ret)
571 return ret;
573 usleep_range(100, 1000);
574 ret = rtsx_usb_write_register(ucr, CLK_DIV, CLK_CHANGE, 0x00);
575 if (ret)
576 return ret;
578 /* determine IC version */
579 ret = rtsx_usb_read_register(ucr, HW_VERSION, &val);
580 if (ret)
581 return ret;
583 ucr->ic_version = val & HW_VER_MASK;
585 /* determine package */
586 ret = rtsx_usb_read_register(ucr, CARD_SHARE_MODE, &val);
587 if (ret)
588 return ret;
590 if (val & CARD_SHARE_LQFP_SEL) {
591 ucr->package = LQFP48;
592 dev_dbg(&ucr->pusb_intf->dev, "Package: LQFP48\n");
593 } else {
594 ucr->package = QFN24;
595 dev_dbg(&ucr->pusb_intf->dev, "Package: QFN24\n");
598 /* determine IC variations */
599 rtsx_usb_read_register(ucr, CFG_MODE_1, &val);
600 if (val & RTS5179) {
601 ucr->is_rts5179 = true;
602 dev_dbg(&ucr->pusb_intf->dev, "Device is rts5179\n");
603 } else {
604 ucr->is_rts5179 = false;
607 return rtsx_usb_reset_chip(ucr);
610 static int rtsx_usb_probe(struct usb_interface *intf,
611 const struct usb_device_id *id)
613 struct usb_device *usb_dev = interface_to_usbdev(intf);
614 struct rtsx_ucr *ucr;
615 int ret;
617 dev_dbg(&intf->dev,
618 ": Realtek USB Card Reader found at bus %03d address %03d\n",
619 usb_dev->bus->busnum, usb_dev->devnum);
621 ucr = devm_kzalloc(&intf->dev, sizeof(*ucr), GFP_KERNEL);
622 if (!ucr)
623 return -ENOMEM;
625 ucr->pusb_dev = usb_dev;
627 ucr->iobuf = usb_alloc_coherent(ucr->pusb_dev, IOBUF_SIZE,
628 GFP_KERNEL, &ucr->iobuf_dma);
629 if (!ucr->iobuf)
630 return -ENOMEM;
632 usb_set_intfdata(intf, ucr);
634 ucr->vendor_id = id->idVendor;
635 ucr->product_id = id->idProduct;
636 ucr->cmd_buf = ucr->rsp_buf = ucr->iobuf;
638 mutex_init(&ucr->dev_mutex);
640 ucr->pusb_intf = intf;
642 /* initialize */
643 ret = rtsx_usb_init_chip(ucr);
644 if (ret)
645 goto out_init_fail;
647 /* initialize USB SG transfer timer */
648 setup_timer(&ucr->sg_timer, rtsx_usb_sg_timed_out, (unsigned long) ucr);
650 ret = mfd_add_hotplug_devices(&intf->dev, rtsx_usb_cells,
651 ARRAY_SIZE(rtsx_usb_cells));
652 if (ret)
653 goto out_init_fail;
655 #ifdef CONFIG_PM
656 intf->needs_remote_wakeup = 1;
657 usb_enable_autosuspend(usb_dev);
658 #endif
660 return 0;
662 out_init_fail:
663 usb_free_coherent(ucr->pusb_dev, IOBUF_SIZE, ucr->iobuf,
664 ucr->iobuf_dma);
665 return ret;
668 static void rtsx_usb_disconnect(struct usb_interface *intf)
670 struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
672 dev_dbg(&intf->dev, "%s called\n", __func__);
674 mfd_remove_devices(&intf->dev);
676 usb_set_intfdata(ucr->pusb_intf, NULL);
677 usb_free_coherent(ucr->pusb_dev, IOBUF_SIZE, ucr->iobuf,
678 ucr->iobuf_dma);
681 #ifdef CONFIG_PM
682 static int rtsx_usb_suspend(struct usb_interface *intf, pm_message_t message)
684 struct rtsx_ucr *ucr =
685 (struct rtsx_ucr *)usb_get_intfdata(intf);
687 dev_dbg(&intf->dev, "%s called with pm message 0x%04x\n",
688 __func__, message.event);
691 * Call to make sure LED is off during suspend to save more power.
692 * It is NOT a permanent state and could be turned on anytime later.
693 * Thus no need to call turn_on when resunming.
695 mutex_lock(&ucr->dev_mutex);
696 rtsx_usb_turn_off_led(ucr);
697 mutex_unlock(&ucr->dev_mutex);
699 return 0;
702 static int rtsx_usb_resume(struct usb_interface *intf)
704 return 0;
707 static int rtsx_usb_reset_resume(struct usb_interface *intf)
709 struct rtsx_ucr *ucr =
710 (struct rtsx_ucr *)usb_get_intfdata(intf);
712 rtsx_usb_reset_chip(ucr);
713 return 0;
716 #else /* CONFIG_PM */
718 #define rtsx_usb_suspend NULL
719 #define rtsx_usb_resume NULL
720 #define rtsx_usb_reset_resume NULL
722 #endif /* CONFIG_PM */
725 static int rtsx_usb_pre_reset(struct usb_interface *intf)
727 struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
729 mutex_lock(&ucr->dev_mutex);
730 return 0;
733 static int rtsx_usb_post_reset(struct usb_interface *intf)
735 struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
737 mutex_unlock(&ucr->dev_mutex);
738 return 0;
741 static struct usb_device_id rtsx_usb_usb_ids[] = {
742 { USB_DEVICE(0x0BDA, 0x0129) },
743 { USB_DEVICE(0x0BDA, 0x0139) },
744 { USB_DEVICE(0x0BDA, 0x0140) },
747 MODULE_DEVICE_TABLE(usb, rtsx_usb_usb_ids);
749 static struct usb_driver rtsx_usb_driver = {
750 .name = "rtsx_usb",
751 .probe = rtsx_usb_probe,
752 .disconnect = rtsx_usb_disconnect,
753 .suspend = rtsx_usb_suspend,
754 .resume = rtsx_usb_resume,
755 .reset_resume = rtsx_usb_reset_resume,
756 .pre_reset = rtsx_usb_pre_reset,
757 .post_reset = rtsx_usb_post_reset,
758 .id_table = rtsx_usb_usb_ids,
759 .supports_autosuspend = 1,
760 .soft_unbind = 1,
763 module_usb_driver(rtsx_usb_driver);
765 MODULE_LICENSE("GPL v2");
766 MODULE_AUTHOR("Roger Tseng <rogerable@realtek.com>");
767 MODULE_DESCRIPTION("Realtek USB Card Reader Driver");