linux/drivers/mfd/rtsx_usb.c
<<
>>
Prefs
   1/* Driver for Realtek USB card reader
   2 *
   3 * Copyright(c) 2009-2013 Realtek Semiconductor Corp. All rights reserved.
   4 *
   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.
   8 *
   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.
  13 *
  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/>.
  16 *
  17 * Author:
  18 *   Roger Tseng <rogerable@realtek.com>
  19 */
  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>
  27
  28static int polling_pipe = 1;
  29module_param(polling_pipe, int, S_IRUGO | S_IWUSR);
  30MODULE_PARM_DESC(polling_pipe, "polling pipe (0: ctl, 1: bulk)");
  31
  32static const struct mfd_cell rtsx_usb_cells[] = {
  33        [RTSX_USB_SD_CARD] = {
  34                .name = "rtsx_usb_sdmmc",
  35                .pdata_size = 0,
  36        },
  37        [RTSX_USB_MS_CARD] = {
  38                .name = "rtsx_usb_ms",
  39                .pdata_size = 0,
  40        },
  41};
  42
  43static void rtsx_usb_sg_timed_out(unsigned long data)
  44{
  45        struct rtsx_ucr *ucr = (struct rtsx_ucr *)data;
  46
  47        dev_dbg(&ucr->pusb_intf->dev, "%s: sg transfer timed out", __func__);
  48        usb_sg_cancel(&ucr->current_sg);
  49
  50        /* we know the cancellation is caused by time-out */
  51        ucr->current_sg.status = -ETIMEDOUT;
  52}
  53
  54static 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)
  57{
  58        int ret;
  59
  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;
  66
  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);
  71
  72        if (act_len)
  73                *act_len = ucr->current_sg.bytes;
  74
  75        return ucr->current_sg.status;
  76}
  77
  78int 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)
  81{
  82        if (timeout < 600)
  83                timeout = 600;
  84
  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);
  92}
  93EXPORT_SYMBOL_GPL(rtsx_usb_transfer_data);
  94
  95static inline void rtsx_usb_seq_cmd_hdr(struct rtsx_ucr *ucr,
  96                u16 addr, u16 len, u8 seq_type)
  97{
  98        rtsx_usb_cmd_hdr_tag(ucr);
  99
 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;
 105
 106        if (seq_type == SEQ_WRITE)
 107                ucr->cmd_buf[STAGE_FLAG] = 0;
 108        else
 109                ucr->cmd_buf[STAGE_FLAG] = STAGE_R;
 110}
 111
 112static int rtsx_usb_seq_write_register(struct rtsx_ucr *ucr,
 113                u16 addr, u16 len, u8 *data)
 114{
 115        u16 cmd_len = ALIGN(SEQ_WRITE_DATA_OFFSET + len, 4);
 116
 117        if (!data)
 118                return -EINVAL;
 119
 120        if (cmd_len > IOBUF_SIZE)
 121                return -EINVAL;
 122
 123        rtsx_usb_seq_cmd_hdr(ucr, addr, len, SEQ_WRITE);
 124        memcpy(ucr->cmd_buf + SEQ_WRITE_DATA_OFFSET, data, len);
 125
 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);
 129}
 130
 131static int rtsx_usb_seq_read_register(struct rtsx_ucr *ucr,
 132                u16 addr, u16 len, u8 *data)
 133{
 134        int i, ret;
 135        u16 rsp_len = round_down(len, 4);
 136        u16 res_len = len - rsp_len;
 137
 138        if (!data)
 139                return -EINVAL;
 140
 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;
 149
 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;
 155        }
 156
 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;
 163        }
 164
 165        return 0;
 166}
 167
 168int rtsx_usb_read_ppbuf(struct rtsx_ucr *ucr, u8 *buf, int buf_len)
 169{
 170        return rtsx_usb_seq_read_register(ucr, PPBUF_BASE2, (u16)buf_len, buf);
 171}
 172EXPORT_SYMBOL_GPL(rtsx_usb_read_ppbuf);
 173
 174int rtsx_usb_write_ppbuf(struct rtsx_ucr *ucr, u8 *buf, int buf_len)
 175{
 176        return rtsx_usb_seq_write_register(ucr, PPBUF_BASE2, (u16)buf_len, buf);
 177}
 178EXPORT_SYMBOL_GPL(rtsx_usb_write_ppbuf);
 179
 180int rtsx_usb_ep0_write_register(struct rtsx_ucr *ucr, u16 addr,
 181                u8 mask, u8 data)
 182{
 183        u16 value, index;
 184
 185        addr |= EP0_WRITE_REG_CMD << EP0_OP_SHIFT;
 186        value = swab16(addr);
 187        index = mask | data << 8;
 188
 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);
 193}
 194EXPORT_SYMBOL_GPL(rtsx_usb_ep0_write_register);
 195
 196int rtsx_usb_ep0_read_register(struct rtsx_ucr *ucr, u16 addr, u8 *data)
 197{
 198        u16 value;
 199
 200        if (!data)
 201                return -EINVAL;
 202        *data = 0;
 203
 204        addr |= EP0_READ_REG_CMD << EP0_OP_SHIFT;
 205        value = swab16(addr);
 206
 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);
 211}
 212EXPORT_SYMBOL_GPL(rtsx_usb_ep0_read_register);
 213
 214void rtsx_usb_add_cmd(struct rtsx_ucr *ucr, u8 cmd_type, u16 reg_addr,
 215                u8 mask, u8 data)
 216{
 217        int i;
 218
 219        if (ucr->cmd_idx < (IOBUF_SIZE - CMD_OFFSET) / 4) {
 220                i = CMD_OFFSET + ucr->cmd_idx * 4;
 221
 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;
 227
 228                ucr->cmd_idx++;
 229        }
 230}
 231EXPORT_SYMBOL_GPL(rtsx_usb_add_cmd);
 232
 233int rtsx_usb_send_cmd(struct rtsx_ucr *ucr, u8 flag, int timeout)
 234{
 235        int ret;
 236
 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;
 240
 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;
 248        }
 249
 250        return 0;
 251}
 252EXPORT_SYMBOL_GPL(rtsx_usb_send_cmd);
 253
 254int rtsx_usb_get_rsp(struct rtsx_ucr *ucr, int rsp_len, int timeout)
 255{
 256        if (rsp_len <= 0)
 257                return -EINVAL;
 258
 259        rsp_len = ALIGN(rsp_len, 4);
 260
 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);
 264}
 265EXPORT_SYMBOL_GPL(rtsx_usb_get_rsp);
 266
 267static int rtsx_usb_get_status_with_bulk(struct rtsx_ucr *ucr, u16 *status)
 268{
 269        int ret;
 270
 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;
 277
 278        ret = rtsx_usb_get_rsp(ucr, 2, 100);
 279        if (ret)
 280                return ret;
 281
 282        *status = ((ucr->rsp_buf[0] >> 2) & 0x0f) |
 283                  ((ucr->rsp_buf[1] & 0x03) << 4);
 284
 285        return 0;
 286}
 287
 288int rtsx_usb_get_card_status(struct rtsx_ucr *ucr, u16 *status)
 289{
 290        int ret;
 291
 292        if (!status)
 293                return -EINVAL;
 294
 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);
 303
 304        /* usb_control_msg may return positive when success */
 305        if (ret < 0)
 306                return ret;
 307
 308        return 0;
 309}
 310EXPORT_SYMBOL_GPL(rtsx_usb_get_card_status);
 311
 312static int rtsx_usb_write_phy_register(struct rtsx_ucr *ucr, u8 addr, u8 val)
 313{
 314        dev_dbg(&ucr->pusb_intf->dev, "Write 0x%x to phy register 0x%x\n",
 315                        val, addr);
 316
 317        rtsx_usb_init_cmd(ucr);
 318
 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);
 329
 330        return rtsx_usb_send_cmd(ucr, MODE_C, 100);
 331}
 332
 333int rtsx_usb_write_register(struct rtsx_ucr *ucr, u16 addr, u8 mask, u8 data)
 334{
 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);
 338}
 339EXPORT_SYMBOL_GPL(rtsx_usb_write_register);
 340
 341int rtsx_usb_read_register(struct rtsx_ucr *ucr, u16 addr, u8 *data)
 342{
 343        int ret;
 344
 345        if (data != NULL)
 346                *data = 0;
 347
 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;
 353
 354        ret = rtsx_usb_get_rsp(ucr, 1, 100);
 355        if (ret)
 356                return ret;
 357
 358        if (data != NULL)
 359                *data = ucr->rsp_buf[0];
 360
 361        return 0;
 362}
 363EXPORT_SYMBOL_GPL(rtsx_usb_read_register);
 364
 365static inline u8 double_ssc_depth(u8 depth)
 366{
 367        return (depth > 1) ? (depth - 1) : depth;
 368}
 369
 370static u8 revise_ssc_depth(u8 ssc_depth, u8 div)
 371{
 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;
 377        }
 378
 379        return ssc_depth;
 380}
 381
 382int rtsx_usb_switch_clock(struct rtsx_ucr *ucr, unsigned int card_clock,
 383                u8 ssc_depth, bool initial_mode, bool double_clk, bool vpclk)
 384{
 385        int ret;
 386        u8 n, clk_divider, mcu_cnt, div;
 387
 388        if (!card_clock) {
 389                ucr->cur_clk = 0;
 390                return 0;
 391        }
 392
 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;
 399        }
 400
 401        ret = rtsx_usb_write_register(ucr, SD_CFG1,
 402                        SD_CLK_DIVIDE_MASK, clk_divider);
 403        if (ret < 0)
 404                return ret;
 405
 406        card_clock /= 1000000;
 407        dev_dbg(&ucr->pusb_intf->dev,
 408                        "Switch card clock to %dMHz\n", card_clock);
 409
 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);
 415
 416        if (card_clock == ucr->cur_clk)
 417                return 0;
 418
 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;
 423
 424        mcu_cnt = 60/card_clock + 3;
 425        if (mcu_cnt > 15)
 426                mcu_cnt = 15;
 427
 428        /* Make sure that the SSC clock div_n is not less than MIN_DIV_N */
 429
 430        div = CLK_DIV_1;
 431        while (n < MIN_DIV_N && div < CLK_DIV_4) {
 432                n = (n + 2) * 2 - 2;
 433                div++;
 434        }
 435        dev_dbg(&ucr->pusb_intf->dev, "n = %d, div = %d\n", n, div);
 436
 437        if (double_clk)
 438                ssc_depth = double_ssc_depth(ssc_depth);
 439
 440        ssc_depth = revise_ssc_depth(ssc_depth, div);
 441        dev_dbg(&ucr->pusb_intf->dev, "ssc_depth = %d\n", ssc_depth);
 442
 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);
 457        }
 458
 459        ret = rtsx_usb_send_cmd(ucr, MODE_C, 2000);
 460        if (ret < 0)
 461                return ret;
 462
 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;
 467
 468        /* Wait SSC clock stable */
 469        usleep_range(100, 1000);
 470
 471        ret = rtsx_usb_write_register(ucr, CLK_DIV, CLK_CHANGE, 0);
 472        if (ret < 0)
 473                return ret;
 474
 475        ucr->cur_clk = card_clock;
 476
 477        return 0;
 478}
 479EXPORT_SYMBOL_GPL(rtsx_usb_switch_clock);
 480
 481int rtsx_usb_card_exclusive_check(struct rtsx_ucr *ucr, int card)
 482{
 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)
 488        };
 489
 490        ret = rtsx_usb_get_card_status(ucr, &val);
 491        /*
 492         * If get status fails, return 0 (ok) for the exclusive check
 493         * and let the flow fail at somewhere else.
 494         */
 495        if (ret)
 496                return 0;
 497
 498        if (val & cd_mask[card])
 499                return -EIO;
 500
 501        return 0;
 502}
 503EXPORT_SYMBOL_GPL(rtsx_usb_card_exclusive_check);
 504
 505static int rtsx_usb_reset_chip(struct rtsx_ucr *ucr)
 506{
 507        int ret;
 508        u8 val;
 509
 510        rtsx_usb_init_cmd(ucr);
 511
 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);
 523        }
 524
 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);
 534
 535        if (ucr->is_rts5179)
 536                rtsx_usb_add_cmd(ucr, WRITE_REG_CMD,
 537                                CARD_PULL_CTL5, 0x03, 0x01);
 538
 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);
 544
 545        ret = rtsx_usb_send_cmd(ucr, MODE_C, 100);
 546        if (ret)
 547                return ret;
 548
 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;
 555        }
 556
 557        return 0;
 558}
 559
 560static int rtsx_usb_init_chip(struct rtsx_ucr *ucr)
 561{
 562        int ret;
 563        u8 val;
 564
 565        rtsx_usb_clear_fsm_err(ucr);
 566
 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;
 572
 573        usleep_range(100, 1000);
 574        ret = rtsx_usb_write_register(ucr, CLK_DIV, CLK_CHANGE, 0x00);
 575        if (ret)
 576                return ret;
 577
 578        /* determine IC version */
 579        ret = rtsx_usb_read_register(ucr, HW_VERSION, &val);
 580        if (ret)
 581                return ret;
 582
 583        ucr->ic_version = val & HW_VER_MASK;
 584
 585        /* determine package */
 586        ret = rtsx_usb_read_register(ucr, CARD_SHARE_MODE, &val);
 587        if (ret)
 588                return ret;
 589
 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");
 596        }
 597
 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;
 605        }
 606
 607        return rtsx_usb_reset_chip(ucr);
 608}
 609
 610static int rtsx_usb_probe(struct usb_interface *intf,
 611                         const struct usb_device_id *id)
 612{
 613        struct usb_device *usb_dev = interface_to_usbdev(intf);
 614        struct rtsx_ucr *ucr;
 615        int ret;
 616
 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);
 620
 621        ucr = devm_kzalloc(&intf->dev, sizeof(*ucr), GFP_KERNEL);
 622        if (!ucr)
 623                return -ENOMEM;
 624
 625        ucr->pusb_dev = usb_dev;
 626
 627        ucr->iobuf = usb_alloc_coherent(ucr->pusb_dev, IOBUF_SIZE,
 628                        GFP_KERNEL, &ucr->iobuf_dma);
 629        if (!ucr->iobuf)
 630                return -ENOMEM;
 631
 632        usb_set_intfdata(intf, ucr);
 633
 634        ucr->vendor_id = id->idVendor;
 635        ucr->product_id = id->idProduct;
 636        ucr->cmd_buf = ucr->rsp_buf = ucr->iobuf;
 637
 638        mutex_init(&ucr->dev_mutex);
 639
 640        ucr->pusb_intf = intf;
 641
 642        /* initialize */
 643        ret = rtsx_usb_init_chip(ucr);
 644        if (ret)
 645                goto out_init_fail;
 646
 647        /* initialize USB SG transfer timer */
 648        setup_timer(&ucr->sg_timer, rtsx_usb_sg_timed_out, (unsigned long) ucr);
 649
 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;
 654
 655#ifdef CONFIG_PM
 656        intf->needs_remote_wakeup = 1;
 657        usb_enable_autosuspend(usb_dev);
 658#endif
 659
 660        return 0;
 661
 662out_init_fail:
 663        usb_free_coherent(ucr->pusb_dev, IOBUF_SIZE, ucr->iobuf,
 664                        ucr->iobuf_dma);
 665        return ret;
 666}
 667
 668static void rtsx_usb_disconnect(struct usb_interface *intf)
 669{
 670        struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
 671
 672        dev_dbg(&intf->dev, "%s called\n", __func__);
 673
 674        mfd_remove_devices(&intf->dev);
 675
 676        usb_set_intfdata(ucr->pusb_intf, NULL);
 677        usb_free_coherent(ucr->pusb_dev, IOBUF_SIZE, ucr->iobuf,
 678                        ucr->iobuf_dma);
 679}
 680
 681#ifdef CONFIG_PM
 682static int rtsx_usb_suspend(struct usb_interface *intf, pm_message_t message)
 683{
 684        dev_dbg(&intf->dev, "%s called with pm message 0x%04x\n",
 685                        __func__, message.event);
 686
 687        return 0;
 688}
 689
 690static int rtsx_usb_resume(struct usb_interface *intf)
 691{
 692        return 0;
 693}
 694
 695static int rtsx_usb_reset_resume(struct usb_interface *intf)
 696{
 697        struct rtsx_ucr *ucr =
 698                (struct rtsx_ucr *)usb_get_intfdata(intf);
 699
 700        rtsx_usb_reset_chip(ucr);
 701        return 0;
 702}
 703
 704#else /* CONFIG_PM */
 705
 706#define rtsx_usb_suspend NULL
 707#define rtsx_usb_resume NULL
 708#define rtsx_usb_reset_resume NULL
 709
 710#endif /* CONFIG_PM */
 711
 712
 713static int rtsx_usb_pre_reset(struct usb_interface *intf)
 714{
 715        struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
 716
 717        mutex_lock(&ucr->dev_mutex);
 718        return 0;
 719}
 720
 721static int rtsx_usb_post_reset(struct usb_interface *intf)
 722{
 723        struct rtsx_ucr *ucr = (struct rtsx_ucr *)usb_get_intfdata(intf);
 724
 725        mutex_unlock(&ucr->dev_mutex);
 726        return 0;
 727}
 728
 729static struct usb_device_id rtsx_usb_usb_ids[] = {
 730        { USB_DEVICE(0x0BDA, 0x0129) },
 731        { USB_DEVICE(0x0BDA, 0x0139) },
 732        { USB_DEVICE(0x0BDA, 0x0140) },
 733        { }
 734};
 735MODULE_DEVICE_TABLE(usb, rtsx_usb_usb_ids);
 736
 737static struct usb_driver rtsx_usb_driver = {
 738        .name                   = "rtsx_usb",
 739        .probe                  = rtsx_usb_probe,
 740        .disconnect             = rtsx_usb_disconnect,
 741        .suspend                = rtsx_usb_suspend,
 742        .resume                 = rtsx_usb_resume,
 743        .reset_resume           = rtsx_usb_reset_resume,
 744        .pre_reset              = rtsx_usb_pre_reset,
 745        .post_reset             = rtsx_usb_post_reset,
 746        .id_table               = rtsx_usb_usb_ids,
 747        .supports_autosuspend   = 1,
 748        .soft_unbind            = 1,
 749};
 750
 751module_usb_driver(rtsx_usb_driver);
 752
 753MODULE_LICENSE("GPL v2");
 754MODULE_AUTHOR("Roger Tseng <rogerable@realtek.com>");
 755MODULE_DESCRIPTION("Realtek USB Card Reader Driver");
 756