smsc_hub.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /* Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/platform_device.h>
  13. #include <linux/err.h>
  14. #include <linux/pm_runtime.h>
  15. #include <linux/regulator/consumer.h>
  16. #include <linux/i2c.h>
  17. #include <linux/gpio.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/of_device.h>
  20. #include <linux/clk.h>
  21. #include <linux/slab.h>
  22. #include <linux/delay.h>
  23. #include <linux/smsc_hub.h>
  24. #include <linux/module.h>
  25. #include <mach/msm_xo.h>
  26. static unsigned short normal_i2c[] = {
  27. 0, I2C_CLIENT_END };
  28. struct hsic_hub {
  29. struct device *dev;
  30. struct smsc_hub_platform_data *pdata;
  31. struct i2c_client *client;
  32. struct msm_xo_voter *xo_handle;
  33. struct clk *ref_clk;
  34. struct regulator *hsic_hub_reg;
  35. struct regulator *int_pad_reg, *hub_vbus_reg;
  36. bool enabled;
  37. };
  38. static struct hsic_hub *smsc_hub;
  39. static struct platform_driver smsc_hub_driver;
  40. /* APIs for setting/clearing bits and for reading/writing values */
  41. static inline int hsic_hub_get_u8(struct i2c_client *client, u8 reg)
  42. {
  43. int ret;
  44. ret = i2c_smbus_read_byte_data(client, reg);
  45. if (ret < 0)
  46. pr_err("%s:i2c_read8 failed\n", __func__);
  47. return ret;
  48. }
  49. static inline int hsic_hub_get_u16(struct i2c_client *client, u8 reg)
  50. {
  51. int ret;
  52. ret = i2c_smbus_read_word_data(client, reg);
  53. if (ret < 0)
  54. pr_err("%s:i2c_read16 failed\n", __func__);
  55. return ret;
  56. }
  57. static inline int hsic_hub_write_word_data(struct i2c_client *client, u8 reg,
  58. u16 value)
  59. {
  60. int ret;
  61. ret = i2c_smbus_write_word_data(client, reg, value);
  62. if (ret)
  63. pr_err("%s:i2c_write16 failed\n", __func__);
  64. return ret;
  65. }
  66. static inline int hsic_hub_write_byte_data(struct i2c_client *client, u8 reg,
  67. u8 value)
  68. {
  69. int ret;
  70. ret = i2c_smbus_write_byte_data(client, reg, value);
  71. if (ret)
  72. pr_err("%s:i2c_write_byte_data failed\n", __func__);
  73. return ret;
  74. }
  75. static inline int hsic_hub_set_bits(struct i2c_client *client, u8 reg,
  76. u8 value)
  77. {
  78. int ret;
  79. ret = i2c_smbus_read_byte_data(client, reg);
  80. if (ret < 0) {
  81. pr_err("%s:i2c_read_byte_data failed\n", __func__);
  82. return ret;
  83. }
  84. return i2c_smbus_write_byte_data(client, reg, (ret | value));
  85. }
  86. static inline int hsic_hub_clear_bits(struct i2c_client *client, u8 reg,
  87. u8 value)
  88. {
  89. int ret;
  90. ret = i2c_smbus_read_byte_data(client, reg);
  91. if (ret < 0) {
  92. pr_err("%s:i2c_read_byte_data failed\n", __func__);
  93. return ret;
  94. }
  95. return i2c_smbus_write_byte_data(client, reg, (ret & ~value));
  96. }
  97. static int smsc4604_send_connect_cmd(struct i2c_client *client)
  98. {
  99. u8 buf[3];
  100. buf[0] = 0xAA;
  101. buf[1] = 0x55;
  102. buf[2] = 0x00;
  103. if (i2c_master_send(client, buf, 3) != 3) {
  104. dev_err(&client->dev, "%s: i2c send failed\n", __func__);
  105. return -EIO;
  106. }
  107. return 0;
  108. }
  109. static int i2c_hsic_hub_probe(struct i2c_client *client,
  110. const struct i2c_device_id *id)
  111. {
  112. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA |
  113. I2C_FUNC_SMBUS_WORD_DATA))
  114. return -EIO;
  115. switch (smsc_hub->pdata->model_id) {
  116. case SMSC3503_ID:
  117. /*
  118. * CONFIG_N bit in SP_ILOCK register has to be set before
  119. * changing other registers to change default configuration
  120. * of hsic hub.
  121. */
  122. hsic_hub_set_bits(client, SMSC3503_SP_ILOCK, CONFIG_N);
  123. /*
  124. * Can change default configuartion like VID,PID,
  125. * strings etc by writing new values to hsic hub registers
  126. */
  127. hsic_hub_write_word_data(client, SMSC3503_VENDORID, 0x05C6);
  128. /*
  129. * CONFIG_N bit in SP_ILOCK register has to be cleared
  130. * for new values in registers to be effective after
  131. * writing to other registers.
  132. */
  133. hsic_hub_clear_bits(client, SMSC3503_SP_ILOCK, CONFIG_N);
  134. break;
  135. case SMSC4604_ID:
  136. /*
  137. * SMSC4604 requires an I2C attach command to be issued
  138. * if I2C bus is connected
  139. */
  140. return smsc4604_send_connect_cmd(client);
  141. default:
  142. return -EINVAL;
  143. }
  144. return 0;
  145. }
  146. static int i2c_hsic_hub_remove(struct i2c_client *client)
  147. {
  148. return 0;
  149. }
  150. static const struct i2c_device_id hsic_hub_id[] = {
  151. {"i2c_hsic_hub", 0},
  152. {}
  153. };
  154. MODULE_DEVICE_TABLE(i2c, hsichub_id);
  155. static struct i2c_driver hsic_hub_driver = {
  156. .driver = {
  157. .name = "i2c_hsic_hub",
  158. },
  159. .probe = i2c_hsic_hub_probe,
  160. .remove = i2c_hsic_hub_remove,
  161. .id_table = hsic_hub_id,
  162. };
  163. static int msm_hsic_hub_init_clock(struct hsic_hub *hub, int init)
  164. {
  165. int ret;
  166. /*
  167. * xo_clk_gpio controls an external xo clock which feeds
  168. * the hub reference clock. When this gpio is present,
  169. * assume that no other clocks are required.
  170. */
  171. if (hub->pdata->xo_clk_gpio)
  172. return 0;
  173. if (!init) {
  174. if (!IS_ERR(hub->ref_clk))
  175. clk_disable_unprepare(hub->ref_clk);
  176. else
  177. msm_xo_put(smsc_hub->xo_handle);
  178. return 0;
  179. }
  180. hub->ref_clk = devm_clk_get(hub->dev, "ref_clk");
  181. if (IS_ERR(hub->ref_clk)) {
  182. dev_dbg(hub->dev, "failed to get ref_clk\n");
  183. /* In the absence of dedicated ref_clk, xo clocks the HUB */
  184. smsc_hub->xo_handle = msm_xo_get(MSM_XO_TCXO_D1, "hsic_hub");
  185. if (IS_ERR(smsc_hub->xo_handle)) {
  186. dev_err(hub->dev, "not able to get the handle\n"
  187. "for TCXO D1 buffer\n");
  188. return PTR_ERR(smsc_hub->xo_handle);
  189. }
  190. ret = msm_xo_mode_vote(smsc_hub->xo_handle, MSM_XO_MODE_ON);
  191. if (ret) {
  192. dev_err(hub->dev, "failed to vote for TCXO\n"
  193. "D1 buffer\n");
  194. msm_xo_put(smsc_hub->xo_handle);
  195. return ret;
  196. }
  197. } else {
  198. ret = clk_prepare_enable(hub->ref_clk);
  199. if (ret)
  200. dev_err(hub->dev, "clk_enable failed for ref_clk\n");
  201. }
  202. return ret;
  203. }
  204. #define HSIC_HUB_INT_VOL_MIN 1800000 /* uV */
  205. #define HSIC_HUB_INT_VOL_MAX 2950000 /* uV */
  206. static int msm_hsic_hub_init_gpio(struct hsic_hub *hub, int init)
  207. {
  208. int ret;
  209. struct smsc_hub_platform_data *pdata = hub->pdata;
  210. if (!init) {
  211. if (!IS_ERR(smsc_hub->int_pad_reg)) {
  212. regulator_disable(smsc_hub->int_pad_reg);
  213. regulator_set_voltage(smsc_hub->int_pad_reg, 0,
  214. HSIC_HUB_INT_VOL_MAX);
  215. }
  216. return 0;
  217. }
  218. ret = devm_gpio_request(hub->dev, pdata->hub_reset, "HSIC_HUB_RESET");
  219. if (ret < 0) {
  220. dev_err(hub->dev, "gpio request failed for GPIO%d\n",
  221. pdata->hub_reset);
  222. return ret;
  223. }
  224. if (pdata->refclk_gpio) {
  225. ret = devm_gpio_request(hub->dev, pdata->refclk_gpio,
  226. "HSIC_HUB_CLK");
  227. if (ret < 0)
  228. dev_err(hub->dev, "gpio request failed (CLK GPIO)\n");
  229. }
  230. if (pdata->xo_clk_gpio) {
  231. ret = devm_gpio_request(hub->dev, pdata->xo_clk_gpio,
  232. "HSIC_HUB_XO_CLK");
  233. if (ret < 0) {
  234. dev_err(hub->dev, "gpio request failed(XO CLK GPIO)\n");
  235. return ret;
  236. }
  237. }
  238. if (pdata->int_gpio) {
  239. ret = devm_gpio_request(hub->dev, pdata->int_gpio,
  240. "HSIC_HUB_INT");
  241. if (ret < 0) {
  242. dev_err(hub->dev, "gpio request failed (INT GPIO)\n");
  243. return ret;
  244. }
  245. /* Enable LDO if required for external pull-up */
  246. smsc_hub->int_pad_reg = devm_regulator_get(hub->dev, "hub-int");
  247. if (IS_ERR(smsc_hub->int_pad_reg)) {
  248. dev_dbg(hub->dev, "unable to get ext hub_int reg\n");
  249. } else {
  250. ret = regulator_set_voltage(smsc_hub->int_pad_reg,
  251. HSIC_HUB_INT_VOL_MIN,
  252. HSIC_HUB_INT_VOL_MAX);
  253. if (ret) {
  254. dev_err(hub->dev, "unable to set the voltage\n"
  255. " for hsic hub int reg\n");
  256. return ret;
  257. }
  258. ret = regulator_enable(smsc_hub->int_pad_reg);
  259. if (ret) {
  260. dev_err(hub->dev, "unable to enable int reg\n");
  261. regulator_set_voltage(smsc_hub->int_pad_reg, 0,
  262. HSIC_HUB_INT_VOL_MAX);
  263. return ret;
  264. }
  265. }
  266. }
  267. return 0;
  268. }
  269. #define HSIC_HUB_VDD_VOL_MIN 1650000 /* uV */
  270. #define HSIC_HUB_VDD_VOL_MAX 1950000 /* uV */
  271. #define HSIC_HUB_VDD_LOAD 36000 /* uA */
  272. static int msm_hsic_hub_init_vdd(struct hsic_hub *hub, int init)
  273. {
  274. int ret;
  275. if (!of_get_property(hub->dev->of_node, "ext-hub-vddio-supply", NULL))
  276. return 0;
  277. if (!init) {
  278. if (!IS_ERR(smsc_hub->hsic_hub_reg)) {
  279. regulator_disable(smsc_hub->hsic_hub_reg);
  280. regulator_set_optimum_mode(smsc_hub->hsic_hub_reg, 0);
  281. regulator_set_voltage(smsc_hub->hsic_hub_reg, 0,
  282. HSIC_HUB_VDD_VOL_MAX);
  283. }
  284. return 0;
  285. }
  286. smsc_hub->hsic_hub_reg = devm_regulator_get(hub->dev, "ext-hub-vddio");
  287. if (IS_ERR(smsc_hub->hsic_hub_reg)) {
  288. dev_dbg(hub->dev, "unable to get ext hub vddcx\n");
  289. } else {
  290. ret = regulator_set_voltage(smsc_hub->hsic_hub_reg,
  291. HSIC_HUB_VDD_VOL_MIN,
  292. HSIC_HUB_VDD_VOL_MAX);
  293. if (ret) {
  294. dev_err(hub->dev, "unable to set the voltage\n"
  295. "for hsic hub reg\n");
  296. return ret;
  297. }
  298. ret = regulator_set_optimum_mode(smsc_hub->hsic_hub_reg,
  299. HSIC_HUB_VDD_LOAD);
  300. if (ret < 0) {
  301. dev_err(hub->dev, "Unable to set mode of VDDCX\n");
  302. goto reg_optimum_mode_fail;
  303. }
  304. ret = regulator_enable(smsc_hub->hsic_hub_reg);
  305. if (ret) {
  306. dev_err(hub->dev, "unable to enable ext hub vddcx\n");
  307. goto reg_enable_fail;
  308. }
  309. }
  310. return 0;
  311. reg_enable_fail:
  312. regulator_set_optimum_mode(smsc_hub->hsic_hub_reg, 0);
  313. reg_optimum_mode_fail:
  314. regulator_set_voltage(smsc_hub->hsic_hub_reg, 0,
  315. HSIC_HUB_VDD_VOL_MAX);
  316. return ret;
  317. }
  318. static int smsc_hub_enable(struct hsic_hub *hub)
  319. {
  320. struct smsc_hub_platform_data *pdata = hub->pdata;
  321. struct of_dev_auxdata *hsic_host_auxdata = dev_get_platdata(hub->dev);
  322. struct device_node *node = hub->dev->of_node;
  323. int ret;
  324. ret = gpio_direction_output(pdata->xo_clk_gpio, 1);
  325. if (ret < 0) {
  326. dev_err(hub->dev, "fail to enable xo clk\n");
  327. return ret;
  328. }
  329. ret = gpio_direction_output(pdata->hub_reset, 0);
  330. if (ret < 0) {
  331. dev_err(hub->dev, "fail to assert reset\n");
  332. goto disable_xo;
  333. }
  334. udelay(5);
  335. ret = gpio_direction_output(pdata->hub_reset, 1);
  336. if (ret < 0) {
  337. dev_err(hub->dev, "fail to de-assert reset\n");
  338. goto disable_xo;
  339. }
  340. ret = of_platform_populate(node, NULL, hsic_host_auxdata,
  341. hub->dev);
  342. if (ret < 0) {
  343. dev_err(smsc_hub->dev, "fail to add child with %d\n",
  344. ret);
  345. goto reset;
  346. }
  347. pm_runtime_allow(hub->dev);
  348. return 0;
  349. reset:
  350. gpio_direction_output(pdata->hub_reset, 0);
  351. disable_xo:
  352. gpio_direction_output(pdata->xo_clk_gpio, 0);
  353. return ret;
  354. }
  355. static int sms_hub_remove_child(struct device *dev, void *data)
  356. {
  357. struct platform_device *pdev = to_platform_device(dev);
  358. /*
  359. * Runtime PM is disabled before the driver's remove method
  360. * is called. So resume the device before unregistering
  361. * the device. Don't worry about the PM usage counter as
  362. * the device will be freed.
  363. */
  364. pm_runtime_get_sync(dev);
  365. of_device_unregister(pdev);
  366. return 0;
  367. }
  368. static int smsc_hub_disable(struct hsic_hub *hub)
  369. {
  370. struct smsc_hub_platform_data *pdata = hub->pdata;
  371. pm_runtime_forbid(hub->dev);
  372. device_for_each_child(hub->dev, NULL, sms_hub_remove_child);
  373. gpio_direction_output(pdata->hub_reset, 0);
  374. gpio_direction_output(pdata->xo_clk_gpio, 0);
  375. return 0;
  376. }
  377. static ssize_t smsc_hub_enable_show(struct device *dev,
  378. struct device_attribute *attr, char *buf)
  379. {
  380. return snprintf(buf, PAGE_SIZE, "%s\n", smsc_hub->enabled ?
  381. "enabled" : "disabled");
  382. }
  383. static ssize_t smsc_hub_enable_store(struct device *dev,
  384. struct device_attribute *attr, const char
  385. *buf, size_t size)
  386. {
  387. bool enable;
  388. int val;
  389. int ret = size;
  390. if (sscanf(buf, "%d", &val) == 1) {
  391. enable = !!val;
  392. } else {
  393. ret = -EINVAL;
  394. goto out;
  395. }
  396. if (smsc_hub->enabled == enable)
  397. goto out;
  398. if (enable)
  399. ret = smsc_hub_enable(smsc_hub);
  400. else
  401. ret = smsc_hub_disable(smsc_hub);
  402. pr_debug("smsc hub %s status %d\n", enable ?
  403. "Enable" : "Disable", ret);
  404. if (!ret) {
  405. ret = size;
  406. smsc_hub->enabled = enable;
  407. }
  408. out:
  409. return ret;
  410. }
  411. static DEVICE_ATTR(enable, S_IRUGO | S_IWUSR, smsc_hub_enable_show,
  412. smsc_hub_enable_store);
  413. struct smsc_hub_platform_data *msm_hub_dt_to_pdata(
  414. struct platform_device *pdev)
  415. {
  416. int rc;
  417. u32 temp_val;
  418. struct device_node *node = pdev->dev.of_node;
  419. struct smsc_hub_platform_data *pdata;
  420. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  421. if (!pdata) {
  422. dev_err(&pdev->dev, "unable to allocate platform data\n");
  423. return ERR_PTR(-ENOMEM);
  424. }
  425. rc = of_property_read_u32(node, "smsc,model-id", &temp_val);
  426. if (rc) {
  427. dev_err(&pdev->dev, "Unable to read smsc,model-id\n");
  428. return ERR_PTR(rc);
  429. } else {
  430. pdata->model_id = temp_val;
  431. if (pdata->model_id == 0)
  432. return pdata;
  433. }
  434. pdata->hub_reset = of_get_named_gpio(node, "smsc,reset-gpio", 0);
  435. if (pdata->hub_reset < 0)
  436. return ERR_PTR(pdata->hub_reset);
  437. pdata->refclk_gpio = of_get_named_gpio(node, "smsc,refclk-gpio", 0);
  438. if (pdata->refclk_gpio < 0)
  439. pdata->refclk_gpio = 0;
  440. pdata->int_gpio = of_get_named_gpio(node, "smsc,int-gpio", 0);
  441. if (pdata->int_gpio < 0)
  442. pdata->int_gpio = 0;
  443. pdata->xo_clk_gpio = of_get_named_gpio(node, "smsc,xo-clk-gpio", 0);
  444. if (pdata->xo_clk_gpio < 0)
  445. pdata->xo_clk_gpio = 0;
  446. return pdata;
  447. }
  448. static int __devinit smsc_hub_probe(struct platform_device *pdev)
  449. {
  450. int ret = 0;
  451. struct smsc_hub_platform_data *pdata;
  452. struct device_node *node = pdev->dev.of_node;
  453. struct i2c_adapter *i2c_adap;
  454. struct i2c_board_info i2c_info;
  455. struct of_dev_auxdata *hsic_host_auxdata;
  456. if (pdev->dev.of_node) {
  457. dev_dbg(&pdev->dev, "device tree enabled\n");
  458. hsic_host_auxdata = dev_get_platdata(&pdev->dev);
  459. pdata = msm_hub_dt_to_pdata(pdev);
  460. if (IS_ERR(pdata))
  461. return PTR_ERR(pdata);
  462. } else {
  463. pdata = pdev->dev.platform_data;
  464. }
  465. if (!pdata) {
  466. dev_err(&pdev->dev, "No platform data\n");
  467. return -ENODEV;
  468. }
  469. if (pdata->model_id == 0) {
  470. dev_dbg(&pdev->dev, "standalone HSIC config enabled\n");
  471. return of_platform_populate(node, NULL,
  472. hsic_host_auxdata, &pdev->dev);
  473. }
  474. if (!pdata->hub_reset)
  475. return -EINVAL;
  476. smsc_hub = devm_kzalloc(&pdev->dev, sizeof(*smsc_hub), GFP_KERNEL);
  477. if (!smsc_hub)
  478. return -ENOMEM;
  479. smsc_hub->dev = &pdev->dev;
  480. smsc_hub->pdata = pdata;
  481. if (of_get_property(pdev->dev.of_node, "hub-vbus-supply", NULL)) {
  482. smsc_hub->hub_vbus_reg = devm_regulator_get(&pdev->dev,
  483. "hub-vbus");
  484. ret = PTR_ERR(smsc_hub->hub_vbus_reg);
  485. if (ret == -EPROBE_DEFER) {
  486. dev_dbg(&pdev->dev, "failed to get hub_vbus\n");
  487. return ret;
  488. }
  489. }
  490. ret = msm_hsic_hub_init_vdd(smsc_hub, 1);
  491. if (ret) {
  492. dev_err(&pdev->dev, "failed to init hub VDD\n");
  493. return ret;
  494. }
  495. ret = msm_hsic_hub_init_clock(smsc_hub, 1);
  496. if (ret) {
  497. dev_err(&pdev->dev, "failed to init hub clock\n");
  498. goto uninit_vdd;
  499. }
  500. ret = msm_hsic_hub_init_gpio(smsc_hub, 1);
  501. if (ret) {
  502. dev_err(&pdev->dev, "failed to init hub gpios\n");
  503. goto uninit_clock;
  504. }
  505. if (pdata->model_id == SMSC3502_ID) {
  506. ret = device_create_file(&pdev->dev, &dev_attr_enable);
  507. if (ret < 0) {
  508. dev_err(&pdev->dev, "fail to create sysfs file\n");
  509. goto uninit_gpio;
  510. }
  511. pm_runtime_forbid(&pdev->dev);
  512. goto done;
  513. }
  514. gpio_direction_output(pdata->hub_reset, 0);
  515. /*
  516. * Hub reset should be asserted for minimum 2microsec
  517. * before deasserting.
  518. */
  519. udelay(5);
  520. gpio_direction_output(pdata->hub_reset, 1);
  521. if (!IS_ERR_OR_NULL(smsc_hub->hub_vbus_reg)) {
  522. ret = regulator_enable(smsc_hub->hub_vbus_reg);
  523. if (ret) {
  524. dev_err(&pdev->dev, "unable to enable hub_vbus\n");
  525. goto uninit_gpio;
  526. }
  527. }
  528. ret = i2c_add_driver(&hsic_hub_driver);
  529. if (ret < 0) {
  530. dev_err(&pdev->dev, "failed to add I2C hsic_hub_driver\n");
  531. goto i2c_add_fail;
  532. }
  533. usleep_range(10000, 12000);
  534. i2c_adap = i2c_get_adapter(SMSC_GSBI_I2C_BUS_ID);
  535. if (!i2c_adap) {
  536. dev_err(&pdev->dev, "failed to get i2c adapter\n");
  537. i2c_del_driver(&hsic_hub_driver);
  538. goto i2c_add_fail;
  539. }
  540. memset(&i2c_info, 0, sizeof(struct i2c_board_info));
  541. strlcpy(i2c_info.type, "i2c_hsic_hub", I2C_NAME_SIZE);
  542. /* 250ms delay is required for SMSC4604 HUB to get I2C up */
  543. msleep(250);
  544. /* Assign I2C slave address per SMSC model */
  545. switch (pdata->model_id) {
  546. case SMSC3503_ID:
  547. normal_i2c[0] = SMSC3503_I2C_ADDR;
  548. break;
  549. case SMSC4604_ID:
  550. normal_i2c[0] = SMSC4604_I2C_ADDR;
  551. break;
  552. default:
  553. dev_err(&pdev->dev, "unsupported SMSC model-id\n");
  554. i2c_put_adapter(i2c_adap);
  555. i2c_del_driver(&hsic_hub_driver);
  556. goto uninit_gpio;
  557. }
  558. smsc_hub->client = i2c_new_probed_device(i2c_adap, &i2c_info,
  559. normal_i2c, NULL);
  560. i2c_put_adapter(i2c_adap);
  561. i2c_add_fail:
  562. ret = of_platform_populate(node, NULL, hsic_host_auxdata, &pdev->dev);
  563. if (ret) {
  564. dev_err(&pdev->dev, "failed to add child node, ret=%d\n", ret);
  565. goto uninit_gpio;
  566. }
  567. smsc_hub->enabled = true;
  568. if (!smsc_hub->client)
  569. dev_err(&pdev->dev,
  570. "failed to connect to smsc_hub through I2C\n");
  571. done:
  572. pm_runtime_set_active(&pdev->dev);
  573. pm_runtime_enable(&pdev->dev);
  574. return 0;
  575. uninit_gpio:
  576. msm_hsic_hub_init_gpio(smsc_hub, 0);
  577. uninit_clock:
  578. msm_hsic_hub_init_clock(smsc_hub, 0);
  579. uninit_vdd:
  580. msm_hsic_hub_init_vdd(smsc_hub, 0);
  581. return ret;
  582. }
  583. static int smsc_hub_remove(struct platform_device *pdev)
  584. {
  585. const struct smsc_hub_platform_data *pdata;
  586. if (!smsc_hub)
  587. return 0;
  588. pdata = smsc_hub->pdata;
  589. if (pdata->model_id == SMSC3502_ID)
  590. device_remove_file(&pdev->dev, &dev_attr_enable);
  591. if (smsc_hub->client) {
  592. i2c_unregister_device(smsc_hub->client);
  593. smsc_hub->client = NULL;
  594. i2c_del_driver(&hsic_hub_driver);
  595. }
  596. pm_runtime_disable(&pdev->dev);
  597. if (!IS_ERR_OR_NULL(smsc_hub->hub_vbus_reg))
  598. regulator_disable(smsc_hub->hub_vbus_reg);
  599. msm_hsic_hub_init_gpio(smsc_hub, 0);
  600. msm_hsic_hub_init_clock(smsc_hub, 0);
  601. msm_hsic_hub_init_vdd(smsc_hub, 0);
  602. return 0;
  603. }
  604. #ifdef CONFIG_PM_RUNTIME
  605. static int msm_smsc_runtime_idle(struct device *dev)
  606. {
  607. dev_dbg(dev, "SMSC HUB runtime idle\n");
  608. return 0;
  609. }
  610. static int smsc_hub_lpm_enter(struct device *dev)
  611. {
  612. int ret = 0;
  613. if (!smsc_hub || !smsc_hub->enabled)
  614. return 0;
  615. if (smsc_hub->xo_handle) {
  616. ret = msm_xo_mode_vote(smsc_hub->xo_handle, MSM_XO_MODE_OFF);
  617. if (ret) {
  618. pr_err("%s: failed to devote for TCXO\n"
  619. "D1 buffer%d\n", __func__, ret);
  620. }
  621. } else if (smsc_hub->pdata->xo_clk_gpio) {
  622. gpio_direction_output(smsc_hub->pdata->xo_clk_gpio, 0);
  623. }
  624. return ret;
  625. }
  626. static int smsc_hub_lpm_exit(struct device *dev)
  627. {
  628. int ret = 0;
  629. if (!smsc_hub || !smsc_hub->enabled)
  630. return 0;
  631. if (smsc_hub->xo_handle) {
  632. ret = msm_xo_mode_vote(smsc_hub->xo_handle, MSM_XO_MODE_ON);
  633. if (ret) {
  634. pr_err("%s: failed to vote for TCXO\n"
  635. "D1 buffer%d\n", __func__, ret);
  636. }
  637. } else if (smsc_hub->pdata->xo_clk_gpio) {
  638. gpio_direction_output(smsc_hub->pdata->xo_clk_gpio, 1);
  639. }
  640. return ret;
  641. }
  642. #endif
  643. #ifdef CONFIG_PM
  644. static const struct dev_pm_ops smsc_hub_dev_pm_ops = {
  645. SET_SYSTEM_SLEEP_PM_OPS(smsc_hub_lpm_enter, smsc_hub_lpm_exit)
  646. SET_RUNTIME_PM_OPS(smsc_hub_lpm_enter, smsc_hub_lpm_exit,
  647. msm_smsc_runtime_idle)
  648. };
  649. #endif
  650. static const struct of_device_id hsic_hub_dt_match[] = {
  651. { .compatible = "qcom,hsic-smsc-hub",
  652. },
  653. {}
  654. };
  655. MODULE_DEVICE_TABLE(of, hsic_hub_dt_match);
  656. static struct platform_driver smsc_hub_driver = {
  657. .driver = {
  658. .name = "msm_smsc_hub",
  659. .owner = THIS_MODULE,
  660. #ifdef CONFIG_PM
  661. .pm = &smsc_hub_dev_pm_ops,
  662. #endif
  663. .of_match_table = hsic_hub_dt_match,
  664. },
  665. .probe = smsc_hub_probe,
  666. .remove = smsc_hub_remove,
  667. };
  668. static int __init smsc_hub_init(void)
  669. {
  670. return platform_driver_register(&smsc_hub_driver);
  671. }
  672. static void __exit smsc_hub_exit(void)
  673. {
  674. platform_driver_unregister(&smsc_hub_driver);
  675. }
  676. subsys_initcall(smsc_hub_init);
  677. module_exit(smsc_hub_exit);
  678. MODULE_LICENSE("GPL v2");
  679. MODULE_DESCRIPTION("SMSC HSIC HUB driver");