Linux 4.1.16
[linux/fpc-iii.git] / drivers / mfd / rtsx_usb.c
blobdbd907d7170ebe990cb63fede374624ea03b6fd4
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;
199 u8 *buf;
200 int ret;
202 if (!data)
203 return -EINVAL;
205 buf = kzalloc(sizeof(u8), GFP_KERNEL);
206 if (!buf)
207 return -ENOMEM;
209 addr |= EP0_READ_REG_CMD << EP0_OP_SHIFT;
210 value = swab16(addr);
212 ret = usb_control_msg(ucr->pusb_dev,
213 usb_rcvctrlpipe(ucr->pusb_dev, 0), RTSX_USB_REQ_REG_OP,
214 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
215 value, 0, buf, 1, 100);
216 *data = *buf;
218 kfree(buf);
219 return ret;
221 EXPORT_SYMBOL_GPL(rtsx_usb_ep0_read_register);
223 void rtsx_usb_add_cmd(struct rtsx_ucr *ucr, u8 cmd_type, u16 reg_addr,
224 u8 mask, u8 data)
226 int i;
228 if (ucr->cmd_idx < (IOBUF_SIZE - CMD_OFFSET) / 4) {
229 i = CMD_OFFSET + ucr->cmd_idx * 4;
231 ucr->cmd_buf[i++] = ((cmd_type & 0x03) << 6) |
232 (u8)((reg_addr >> 8) & 0x3F);
233 ucr->cmd_buf[i++] = (u8)reg_addr;
234 ucr->cmd_buf[i++] = mask;
235 ucr->cmd_buf[i++] = data;
237 ucr->cmd_idx++;
240 EXPORT_SYMBOL_GPL(rtsx_usb_add_cmd);
242 int rtsx_usb_send_cmd(struct rtsx_ucr *ucr, u8 flag, int timeout)
244 int ret;
246 ucr->cmd_buf[CNT_H] = (u8)(ucr->cmd_idx >> 8);
247 ucr->cmd_buf[CNT_L] = (u8)(ucr->cmd_idx);
248 ucr->cmd_buf[STAGE_FLAG] = flag;
250 ret = rtsx_usb_transfer_data(ucr,
251 usb_sndbulkpipe(ucr->pusb_dev, EP_BULK_OUT),
252 ucr->cmd_buf, ucr->cmd_idx * 4 + CMD_OFFSET,
253 0, NULL, timeout);
254 if (ret) {
255 rtsx_usb_clear_fsm_err(ucr);
256 return ret;
259 return 0;
261 EXPORT_SYMBOL_GPL(rtsx_usb_send_cmd);
263 int rtsx_usb_get_rsp(struct rtsx_ucr *ucr, int rsp_len, int timeout)
265 if (rsp_len <= 0)
266 return -EINVAL;
268 rsp_len = ALIGN(rsp_len, 4);
270 return rtsx_usb_transfer_data(ucr,
271 usb_rcvbulkpipe(ucr->pusb_dev, EP_BULK_IN),
272 ucr->rsp_buf, rsp_len, 0, NULL, timeout);
274 EXPORT_SYMBOL_GPL(rtsx_usb_get_rsp);
276 static int rtsx_usb_get_status_with_bulk(struct rtsx_ucr *ucr, u16 *status)
278 int ret;
280 rtsx_usb_init_cmd(ucr);
281 rtsx_usb_add_cmd(ucr, READ_REG_CMD, CARD_EXIST, 0x00, 0x00);
282 rtsx_usb_add_cmd(ucr, READ_REG_CMD, OCPSTAT, 0x00, 0x00);
283 ret = rtsx_usb_send_cmd(ucr, MODE_CR, 100);
284 if (ret)
285 return ret;
287 ret = rtsx_usb_get_rsp(ucr, 2, 100);
288 if (ret)
289 return ret;
291 *status = ((ucr->rsp_buf[0] >> 2) & 0x0f) |
292 ((ucr->rsp_buf[1] & 0x03) << 4);
294 return 0;
297 int rtsx_usb_get_card_status(struct rtsx_ucr *ucr, u16 *status)
299 int ret;
300 u16 *buf;
302 if (!status)
303 return -EINVAL;
305 if (polling_pipe == 0) {
306 buf = kzalloc(sizeof(u16), GFP_KERNEL);
307 if (!buf)
308 return -ENOMEM;
310 ret = usb_control_msg(ucr->pusb_dev,
311 usb_rcvctrlpipe(ucr->pusb_dev, 0),
312 RTSX_USB_REQ_POLL,
313 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
314 0, 0, buf, 2, 100);
315 *status = *buf;
317 kfree(buf);
318 } else {
319 ret = rtsx_usb_get_status_with_bulk(ucr, status);
322 /* usb_control_msg may return positive when success */
323 if (ret < 0)
324 return ret;
326 return 0;
328 EXPORT_SYMBOL_GPL(rtsx_usb_get_card_status);
330 static int rtsx_usb_write_phy_register(struct rtsx_ucr *ucr, u8 addr, u8 val)
332 dev_dbg(&ucr->pusb_intf->dev, "Write 0x%x to phy register 0x%x\n",
333 val, addr);
335 rtsx_usb_init_cmd(ucr);
337 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VSTAIN, 0xFF, val);
338 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VCONTROL, 0xFF, addr & 0x0F);
339 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00);
340 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00);
341 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x01);
342 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VCONTROL,
343 0xFF, (addr >> 4) & 0x0F);
344 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00);
345 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x00);
346 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, HS_VLOADM, 0xFF, 0x01);
348 return rtsx_usb_send_cmd(ucr, MODE_C, 100);
351 int rtsx_usb_write_register(struct rtsx_ucr *ucr, u16 addr, u8 mask, u8 data)
353 rtsx_usb_init_cmd(ucr);
354 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, addr, mask, data);
355 return rtsx_usb_send_cmd(ucr, MODE_C, 100);
357 EXPORT_SYMBOL_GPL(rtsx_usb_write_register);
359 int rtsx_usb_read_register(struct rtsx_ucr *ucr, u16 addr, u8 *data)
361 int ret;
363 if (data != NULL)
364 *data = 0;
366 rtsx_usb_init_cmd(ucr);
367 rtsx_usb_add_cmd(ucr, READ_REG_CMD, addr, 0, 0);
368 ret = rtsx_usb_send_cmd(ucr, MODE_CR, 100);
369 if (ret)
370 return ret;
372 ret = rtsx_usb_get_rsp(ucr, 1, 100);
373 if (ret)
374 return ret;
376 if (data != NULL)
377 *data = ucr->rsp_buf[0];
379 return 0;
381 EXPORT_SYMBOL_GPL(rtsx_usb_read_register);
383 static inline u8 double_ssc_depth(u8 depth)
385 return (depth > 1) ? (depth - 1) : depth;
388 static u8 revise_ssc_depth(u8 ssc_depth, u8 div)
390 if (div > CLK_DIV_1) {
391 if (ssc_depth > div - 1)
392 ssc_depth -= (div - 1);
393 else
394 ssc_depth = SSC_DEPTH_2M;
397 return ssc_depth;
400 int rtsx_usb_switch_clock(struct rtsx_ucr *ucr, unsigned int card_clock,
401 u8 ssc_depth, bool initial_mode, bool double_clk, bool vpclk)
403 int ret;
404 u8 n, clk_divider, mcu_cnt, div;
406 if (!card_clock) {
407 ucr->cur_clk = 0;
408 return 0;
411 if (initial_mode) {
412 /* We use 250k(around) here, in initial stage */
413 clk_divider = SD_CLK_DIVIDE_128;
414 card_clock = 30000000;
415 } else {
416 clk_divider = SD_CLK_DIVIDE_0;
419 ret = rtsx_usb_write_register(ucr, SD_CFG1,
420 SD_CLK_DIVIDE_MASK, clk_divider);
421 if (ret < 0)
422 return ret;
424 card_clock /= 1000000;
425 dev_dbg(&ucr->pusb_intf->dev,
426 "Switch card clock to %dMHz\n", card_clock);
428 if (!initial_mode && double_clk)
429 card_clock *= 2;
430 dev_dbg(&ucr->pusb_intf->dev,
431 "Internal SSC clock: %dMHz (cur_clk = %d)\n",
432 card_clock, ucr->cur_clk);
434 if (card_clock == ucr->cur_clk)
435 return 0;
437 /* Converting clock value into internal settings: n and div */
438 n = card_clock - 2;
439 if ((card_clock <= 2) || (n > MAX_DIV_N))
440 return -EINVAL;
442 mcu_cnt = 60/card_clock + 3;
443 if (mcu_cnt > 15)
444 mcu_cnt = 15;
446 /* Make sure that the SSC clock div_n is not less than MIN_DIV_N */
448 div = CLK_DIV_1;
449 while (n < MIN_DIV_N && div < CLK_DIV_4) {
450 n = (n + 2) * 2 - 2;
451 div++;
453 dev_dbg(&ucr->pusb_intf->dev, "n = %d, div = %d\n", n, div);
455 if (double_clk)
456 ssc_depth = double_ssc_depth(ssc_depth);
458 ssc_depth = revise_ssc_depth(ssc_depth, div);
459 dev_dbg(&ucr->pusb_intf->dev, "ssc_depth = %d\n", ssc_depth);
461 rtsx_usb_init_cmd(ucr);
462 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CLK_DIV, CLK_CHANGE, CLK_CHANGE);
463 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CLK_DIV,
464 0x3F, (div << 4) | mcu_cnt);
465 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, 0);
466 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_CTL2,
467 SSC_DEPTH_MASK, ssc_depth);
468 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_DIV_N_0, 0xFF, n);
469 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SSC_CTL1, SSC_RSTB, SSC_RSTB);
470 if (vpclk) {
471 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SD_VPCLK0_CTL,
472 PHASE_NOT_RESET, 0);
473 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SD_VPCLK0_CTL,
474 PHASE_NOT_RESET, PHASE_NOT_RESET);
477 ret = rtsx_usb_send_cmd(ucr, MODE_C, 2000);
478 if (ret < 0)
479 return ret;
481 ret = rtsx_usb_write_register(ucr, SSC_CTL1, 0xff,
482 SSC_RSTB | SSC_8X_EN | SSC_SEL_4M);
483 if (ret < 0)
484 return ret;
486 /* Wait SSC clock stable */
487 usleep_range(100, 1000);
489 ret = rtsx_usb_write_register(ucr, CLK_DIV, CLK_CHANGE, 0);
490 if (ret < 0)
491 return ret;
493 ucr->cur_clk = card_clock;
495 return 0;
497 EXPORT_SYMBOL_GPL(rtsx_usb_switch_clock);
499 int rtsx_usb_card_exclusive_check(struct rtsx_ucr *ucr, int card)
501 int ret;
502 u16 val;
503 u16 cd_mask[] = {
504 [RTSX_USB_SD_CARD] = (CD_MASK & ~SD_CD),
505 [RTSX_USB_MS_CARD] = (CD_MASK & ~MS_CD)
508 ret = rtsx_usb_get_card_status(ucr, &val);
510 * If get status fails, return 0 (ok) for the exclusive check
511 * and let the flow fail at somewhere else.
513 if (ret)
514 return 0;
516 if (val & cd_mask[card])
517 return -EIO;
519 return 0;
521 EXPORT_SYMBOL_GPL(rtsx_usb_card_exclusive_check);
523 static int rtsx_usb_reset_chip(struct rtsx_ucr *ucr)
525 int ret;
526 u8 val;
528 rtsx_usb_init_cmd(ucr);
530 if (CHECK_PKG(ucr, LQFP48)) {
531 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PWR_CTL,
532 LDO3318_PWR_MASK, LDO_SUSPEND);
533 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PWR_CTL,
534 FORCE_LDO_POWERB, FORCE_LDO_POWERB);
535 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL1,
536 0x30, 0x10);
537 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL5,
538 0x03, 0x01);
539 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_PULL_CTL6,
540 0x0C, 0x04);
543 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SYS_DUMMY0, NYET_MSAK, NYET_EN);
544 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CD_DEGLITCH_WIDTH, 0xFF, 0x08);
545 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD,
546 CD_DEGLITCH_EN, XD_CD_DEGLITCH_EN, 0x0);
547 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, SD30_DRIVE_SEL,
548 SD30_DRIVE_MASK, DRIVER_TYPE_D);
549 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD,
550 CARD_DRIVE_SEL, SD20_DRIVE_MASK, 0x0);
551 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, LDO_POWER_CFG, 0xE0, 0x0);
553 if (ucr->is_rts5179)
554 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD,
555 CARD_PULL_CTL5, 0x03, 0x01);
557 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_DMA1_CTL,
558 EXTEND_DMA1_ASYNC_SIGNAL, EXTEND_DMA1_ASYNC_SIGNAL);
559 rtsx_usb_add_cmd(ucr, WRITE_REG_CMD, CARD_INT_PEND,
560 XD_INT | MS_INT | SD_INT,
561 XD_INT | MS_INT | SD_INT);
563 ret = rtsx_usb_send_cmd(ucr, MODE_C, 100);
564 if (ret)
565 return ret;
567 /* config non-crystal mode */
568 rtsx_usb_read_register(ucr, CFG_MODE, &val);
569 if ((val & XTAL_FREE) || ((val & CLK_MODE_MASK) == CLK_MODE_NON_XTAL)) {
570 ret = rtsx_usb_write_phy_register(ucr, 0xC2, 0x7C);
571 if (ret)
572 return ret;
575 return 0;
578 static int rtsx_usb_init_chip(struct rtsx_ucr *ucr)
580 int ret;
581 u8 val;
583 rtsx_usb_clear_fsm_err(ucr);
585 /* power on SSC */
586 ret = rtsx_usb_write_register(ucr,
587 FPDCTL, SSC_POWER_MASK, SSC_POWER_ON);
588 if (ret)
589 return ret;
591 usleep_range(100, 1000);
592 ret = rtsx_usb_write_register(ucr, CLK_DIV, CLK_CHANGE, 0x00);
593 if (ret)
594 return ret;
596 /* determine IC version */
597 ret = rtsx_usb_read_register(ucr, HW_VERSION, &val);
598 if (ret)
599 return ret;
601 ucr->ic_version = val & HW_VER_MASK;
603 /* determine package */
604 ret = rtsx_usb_read_register(ucr, CARD_SHARE_MODE, &val);
605 if (ret)
606 return ret;
608 if (val & CARD_SHARE_LQFP_SEL) {
609 ucr->package = LQFP48;
610 dev_dbg(&ucr->pusb_intf->dev, "Package: LQFP48\n");
611 } else {
612 ucr->package = QFN24;
613 dev_dbg(&ucr->pusb_intf->dev, "Package: QFN24\n");
616 /* determine IC variations */
617 rtsx_usb_read_register(ucr, CFG_MODE_1, &val);
618 if (val & RTS5179) {
619 ucr->is_rts5179 = true;
620 dev_dbg(&ucr->pusb_intf->dev, "Device is rts5179\n");
621 } else {
622 ucr->is_rts5179 = false;
625 return rtsx_usb_reset_chip(ucr);
628 static int rtsx_usb_probe(struct usb_interface *intf,
629 const struct usb_device_id *id)
631 struct usb_device *usb_dev = interface_to_usbdev(intf);
632 struct rtsx_ucr *ucr;
633 int ret;
635 dev_dbg(&intf->dev,
636 ": Realtek USB Card Reader found at bus %03d address %03d\n",
637 usb_dev->bus->busnum, usb_dev->devnum);
639 ucr = devm_kzalloc(&intf->dev, sizeof(*ucr), GFP_KERNEL);
640 if (!ucr)
641 return -ENOMEM;
643 ucr->pusb_dev = usb_dev;
645 ucr->iobuf = usb_alloc_coherent(ucr->pusb_dev, IOBUF_SIZE,
646 GFP_KERNEL, &ucr->iobuf_dma);
647 if (!ucr->iobuf)
648 return -ENOMEM;
650 usb_set_intfdata(intf, ucr);
652 ucr->vendor_id = id->idVendor;
653 ucr->product_id = id->idProduct;
654 ucr->cmd_buf = ucr->rsp_buf = ucr->iobuf;
656 mutex_init(&ucr->dev_mutex);
658 ucr->pusb_intf = intf;
660 /* initialize */
661 ret = rtsx_usb_init_chip(ucr);
662 if (ret)
663 goto out_init_fail;
665 /* initialize USB SG transfer timer */
666 setup_timer(&ucr->sg_timer, rtsx_usb_sg_timed_out, (unsigned long) ucr);
668 ret = mfd_add_hotplug_devices(&intf->dev, rtsx_usb_cells,
669 ARRAY_SIZE(rtsx_usb_cells));
670 if (ret)
671 goto out_init_fail;
673 #ifdef CONFIG_PM
674 intf->needs_remote_wakeup = 1;
675 usb_enable_autosuspend(usb_dev);
676 #endif
678 return 0;
680 out_init_fail:
681 usb_free_coherent(ucr->pusb_dev, IOBUF_SIZE, ucr->iobuf,
682 ucr->iobuf_dma);
683 return ret;
686 static void rtsx_usb_disconnect(struct usb_interface *intf)
688 struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
690 dev_dbg(&intf->dev, "%s called\n", __func__);
692 mfd_remove_devices(&intf->dev);
694 usb_set_intfdata(ucr->pusb_intf, NULL);
695 usb_free_coherent(ucr->pusb_dev, IOBUF_SIZE, ucr->iobuf,
696 ucr->iobuf_dma);
699 #ifdef CONFIG_PM
700 static int rtsx_usb_suspend(struct usb_interface *intf, pm_message_t message)
702 struct rtsx_ucr *ucr =
703 (struct rtsx_ucr *)usb_get_intfdata(intf);
704 u16 val = 0;
706 dev_dbg(&intf->dev, "%s called with pm message 0x%04x\n",
707 __func__, message.event);
709 if (PMSG_IS_AUTO(message)) {
710 if (mutex_trylock(&ucr->dev_mutex)) {
711 rtsx_usb_get_card_status(ucr, &val);
712 mutex_unlock(&ucr->dev_mutex);
714 /* Defer the autosuspend if card exists */
715 if (val & (SD_CD | MS_CD))
716 return -EAGAIN;
717 } else {
718 /* There is an ongoing operation*/
719 return -EAGAIN;
723 return 0;
726 static int rtsx_usb_resume(struct usb_interface *intf)
728 return 0;
731 static int rtsx_usb_reset_resume(struct usb_interface *intf)
733 struct rtsx_ucr *ucr =
734 (struct rtsx_ucr *)usb_get_intfdata(intf);
736 rtsx_usb_reset_chip(ucr);
737 return 0;
740 #else /* CONFIG_PM */
742 #define rtsx_usb_suspend NULL
743 #define rtsx_usb_resume NULL
744 #define rtsx_usb_reset_resume NULL
746 #endif /* CONFIG_PM */
749 static int rtsx_usb_pre_reset(struct usb_interface *intf)
751 struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
753 mutex_lock(&ucr->dev_mutex);
754 return 0;
757 static int rtsx_usb_post_reset(struct usb_interface *intf)
759 struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
761 mutex_unlock(&ucr->dev_mutex);
762 return 0;
765 static struct usb_device_id rtsx_usb_usb_ids[] = {
766 { USB_DEVICE(0x0BDA, 0x0129) },
767 { USB_DEVICE(0x0BDA, 0x0139) },
768 { USB_DEVICE(0x0BDA, 0x0140) },
771 MODULE_DEVICE_TABLE(usb, rtsx_usb_usb_ids);
773 static struct usb_driver rtsx_usb_driver = {
774 .name = "rtsx_usb",
775 .probe = rtsx_usb_probe,
776 .disconnect = rtsx_usb_disconnect,
777 .suspend = rtsx_usb_suspend,
778 .resume = rtsx_usb_resume,
779 .reset_resume = rtsx_usb_reset_resume,
780 .pre_reset = rtsx_usb_pre_reset,
781 .post_reset = rtsx_usb_post_reset,
782 .id_table = rtsx_usb_usb_ids,
783 .supports_autosuspend = 1,
784 .soft_unbind = 1,
787 module_usb_driver(rtsx_usb_driver);
789 MODULE_LICENSE("GPL v2");
790 MODULE_AUTHOR("Roger Tseng <rogerable@realtek.com>");
791 MODULE_DESCRIPTION("Realtek USB Card Reader Driver");