rts5229.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* Driver for Realtek PCI-Express card reader
  2. *
  3. * Copyright(c) 2009 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 as published by the
  7. * Free Software Foundation; either version 2, or (at your option) any
  8. * later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Author:
  19. * Wei WANG <wei_wang@realsil.com.cn>
  20. * No. 450, Shenhu Road, Suzhou Industry Park, Suzhou, China
  21. */
  22. #include <linux/module.h>
  23. #include <linux/delay.h>
  24. #include <linux/mfd/rtsx_pci.h>
  25. #include "rtsx_pcr.h"
  26. static u8 rts5229_get_ic_version(struct rtsx_pcr *pcr)
  27. {
  28. u8 val;
  29. rtsx_pci_read_register(pcr, DUMMY_REG_RESET_0, &val);
  30. return val & 0x0F;
  31. }
  32. static int rts5229_extra_init_hw(struct rtsx_pcr *pcr)
  33. {
  34. rtsx_pci_init_cmd(pcr);
  35. /* Configure GPIO as output */
  36. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, GPIO_CTL, 0x02, 0x02);
  37. /* Switch LDO3318 source from DV33 to card_3v3 */
  38. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x00);
  39. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, LDO_PWR_SEL, 0x03, 0x01);
  40. /* LED shine disabled, set initial shine cycle period */
  41. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, OLT_LED_CTL, 0x0F, 0x02);
  42. return rtsx_pci_send_cmd(pcr, 100);
  43. }
  44. static int rts5229_optimize_phy(struct rtsx_pcr *pcr)
  45. {
  46. /* Optimize RX sensitivity */
  47. return rtsx_pci_write_phy_register(pcr, 0x00, 0xBA42);
  48. }
  49. static int rts5229_turn_on_led(struct rtsx_pcr *pcr)
  50. {
  51. return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x02);
  52. }
  53. static int rts5229_turn_off_led(struct rtsx_pcr *pcr)
  54. {
  55. return rtsx_pci_write_register(pcr, GPIO_CTL, 0x02, 0x00);
  56. }
  57. static int rts5229_enable_auto_blink(struct rtsx_pcr *pcr)
  58. {
  59. return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x08);
  60. }
  61. static int rts5229_disable_auto_blink(struct rtsx_pcr *pcr)
  62. {
  63. return rtsx_pci_write_register(pcr, OLT_LED_CTL, 0x08, 0x00);
  64. }
  65. static int rts5229_card_power_on(struct rtsx_pcr *pcr, int card)
  66. {
  67. int err;
  68. rtsx_pci_init_cmd(pcr);
  69. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  70. SD_POWER_MASK, SD_PARTIAL_POWER_ON);
  71. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  72. LDO3318_PWR_MASK, 0x02);
  73. err = rtsx_pci_send_cmd(pcr, 100);
  74. if (err < 0)
  75. return err;
  76. /* To avoid too large in-rush current */
  77. udelay(150);
  78. rtsx_pci_init_cmd(pcr);
  79. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  80. SD_POWER_MASK, SD_POWER_ON);
  81. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  82. LDO3318_PWR_MASK, 0x06);
  83. err = rtsx_pci_send_cmd(pcr, 100);
  84. if (err < 0)
  85. return err;
  86. return 0;
  87. }
  88. static int rts5229_card_power_off(struct rtsx_pcr *pcr, int card)
  89. {
  90. rtsx_pci_init_cmd(pcr);
  91. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, CARD_PWR_CTL,
  92. SD_POWER_MASK | PMOS_STRG_MASK,
  93. SD_POWER_OFF | PMOS_STRG_400mA);
  94. rtsx_pci_add_cmd(pcr, WRITE_REG_CMD, PWR_GATE_CTRL,
  95. LDO3318_PWR_MASK, 0X00);
  96. return rtsx_pci_send_cmd(pcr, 100);
  97. }
  98. static int rts5229_switch_output_voltage(struct rtsx_pcr *pcr, u8 voltage)
  99. {
  100. int err;
  101. if (voltage == OUTPUT_3V3) {
  102. err = rtsx_pci_write_register(pcr,
  103. SD30_DRIVE_SEL, 0x07, DRIVER_TYPE_D);
  104. if (err < 0)
  105. return err;
  106. err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4FC0 | 0x24);
  107. if (err < 0)
  108. return err;
  109. } else if (voltage == OUTPUT_1V8) {
  110. err = rtsx_pci_write_register(pcr,
  111. SD30_DRIVE_SEL, 0x07, DRIVER_TYPE_B);
  112. if (err < 0)
  113. return err;
  114. err = rtsx_pci_write_phy_register(pcr, 0x08, 0x4C40 | 0x24);
  115. if (err < 0)
  116. return err;
  117. } else {
  118. return -EINVAL;
  119. }
  120. return 0;
  121. }
  122. static const struct pcr_ops rts5229_pcr_ops = {
  123. .extra_init_hw = rts5229_extra_init_hw,
  124. .optimize_phy = rts5229_optimize_phy,
  125. .turn_on_led = rts5229_turn_on_led,
  126. .turn_off_led = rts5229_turn_off_led,
  127. .enable_auto_blink = rts5229_enable_auto_blink,
  128. .disable_auto_blink = rts5229_disable_auto_blink,
  129. .card_power_on = rts5229_card_power_on,
  130. .card_power_off = rts5229_card_power_off,
  131. .switch_output_voltage = rts5229_switch_output_voltage,
  132. .cd_deglitch = NULL,
  133. .conv_clk_and_div_n = NULL,
  134. };
  135. /* SD Pull Control Enable:
  136. * SD_DAT[3:0] ==> pull up
  137. * SD_CD ==> pull up
  138. * SD_WP ==> pull up
  139. * SD_CMD ==> pull up
  140. * SD_CLK ==> pull down
  141. */
  142. static const u32 rts5229_sd_pull_ctl_enable_tbl1[] = {
  143. RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
  144. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE9),
  145. 0,
  146. };
  147. /* For RTS5229 version C */
  148. static const u32 rts5229_sd_pull_ctl_enable_tbl2[] = {
  149. RTSX_REG_PAIR(CARD_PULL_CTL2, 0xAA),
  150. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD9),
  151. 0,
  152. };
  153. /* SD Pull Control Disable:
  154. * SD_DAT[3:0] ==> pull down
  155. * SD_CD ==> pull up
  156. * SD_WP ==> pull down
  157. * SD_CMD ==> pull down
  158. * SD_CLK ==> pull down
  159. */
  160. static const u32 rts5229_sd_pull_ctl_disable_tbl1[] = {
  161. RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
  162. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xD5),
  163. 0,
  164. };
  165. /* For RTS5229 version C */
  166. static const u32 rts5229_sd_pull_ctl_disable_tbl2[] = {
  167. RTSX_REG_PAIR(CARD_PULL_CTL2, 0x55),
  168. RTSX_REG_PAIR(CARD_PULL_CTL3, 0xE5),
  169. 0,
  170. };
  171. /* MS Pull Control Enable:
  172. * MS CD ==> pull up
  173. * others ==> pull down
  174. */
  175. static const u32 rts5229_ms_pull_ctl_enable_tbl[] = {
  176. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
  177. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
  178. 0,
  179. };
  180. /* MS Pull Control Disable:
  181. * MS CD ==> pull up
  182. * others ==> pull down
  183. */
  184. static const u32 rts5229_ms_pull_ctl_disable_tbl[] = {
  185. RTSX_REG_PAIR(CARD_PULL_CTL5, 0x55),
  186. RTSX_REG_PAIR(CARD_PULL_CTL6, 0x15),
  187. 0,
  188. };
  189. void rts5229_init_params(struct rtsx_pcr *pcr)
  190. {
  191. pcr->extra_caps = EXTRA_CAPS_SD_SDR50 | EXTRA_CAPS_SD_SDR104;
  192. pcr->num_slots = 2;
  193. pcr->ops = &rts5229_pcr_ops;
  194. pcr->ic_version = rts5229_get_ic_version(pcr);
  195. if (pcr->ic_version == IC_VER_C) {
  196. pcr->sd_pull_ctl_enable_tbl = rts5229_sd_pull_ctl_enable_tbl2;
  197. pcr->sd_pull_ctl_disable_tbl = rts5229_sd_pull_ctl_disable_tbl2;
  198. } else {
  199. pcr->sd_pull_ctl_enable_tbl = rts5229_sd_pull_ctl_enable_tbl1;
  200. pcr->sd_pull_ctl_disable_tbl = rts5229_sd_pull_ctl_disable_tbl1;
  201. }
  202. pcr->ms_pull_ctl_enable_tbl = rts5229_ms_pull_ctl_enable_tbl;
  203. pcr->ms_pull_ctl_disable_tbl = rts5229_ms_pull_ctl_disable_tbl;
  204. }