marimba-core.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  1. /* Copyright (c) 2009-2012, 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. */
  13. /*
  14. * Qualcomm Marimba Core Driver
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/mutex.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/err.h>
  21. #include <linux/i2c.h>
  22. #include <linux/mfd/marimba.h>
  23. #include <linux/slab.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/module.h>
  26. #define MARIMBA_MODE 0x00
  27. #define ADIE_ARRY_SIZE (CHIP_ID_MAX * MARIMBA_NUM_CHILD)
  28. static int marimba_shadow[ADIE_ARRY_SIZE][0xff];
  29. static int mutex_initialized;
  30. struct marimba marimba_modules[ADIE_ARRY_SIZE];
  31. #define MARIMBA_VERSION_REG 0x11
  32. #define MARIMBA_MODE_REG 0x00
  33. struct marimba_platform_data *marimba_pdata;
  34. static uint32_t marimba_gpio_count;
  35. static bool fm_status;
  36. static bool bt_status;
  37. #ifdef CONFIG_I2C_SSBI
  38. #define NUM_ADD MARIMBA_NUM_CHILD
  39. #else
  40. #define NUM_ADD (MARIMBA_NUM_CHILD - 1)
  41. #endif
  42. #if defined(CONFIG_DEBUG_FS)
  43. struct adie_dbg_device {
  44. struct mutex dbg_mutex;
  45. struct dentry *dent;
  46. int addr;
  47. int mod_id;
  48. };
  49. static struct adie_dbg_device *marimba_dbg_device;
  50. static struct adie_dbg_device *timpani_dbg_device;
  51. static struct adie_dbg_device *bahama_dbg_device;
  52. #endif
  53. /**
  54. * marimba_read_bahama_ver - Reads Bahama version.
  55. * @param marimba: marimba structure pointer passed by client
  56. * @returns result of the operation.
  57. */
  58. int marimba_read_bahama_ver(struct marimba *marimba)
  59. {
  60. int rc;
  61. u8 bahama_version;
  62. rc = marimba_read_bit_mask(marimba, 0x00, &bahama_version, 1, 0x1F);
  63. if (rc < 0)
  64. return rc;
  65. pr_debug("%s: Bahama version: 0x%x\n", __func__, bahama_version);
  66. switch (bahama_version) {
  67. case 0x08: /* varient of bahama v1 */
  68. case 0x10:
  69. case 0x00:
  70. return BAHAMA_VER_1_0;
  71. case 0x09: /* variant of bahama v2 */
  72. case 0x0a: /* variant of bahama v2.1 */
  73. /* Falling through because initialization */
  74. /* and configuration for 2.0 and 2.1 are same */
  75. return BAHAMA_VER_2_0;
  76. default:
  77. return BAHAMA_VER_UNSUPPORTED;
  78. }
  79. }
  80. EXPORT_SYMBOL(marimba_read_bahama_ver);
  81. /**
  82. * marimba_ssbi_write - Writes a n bit TSADC register in Marimba
  83. * @param marimba: marimba structure pointer passed by client
  84. * @param reg: register address
  85. * @param value: buffer to be written
  86. * @param len: num of bytes
  87. * @returns result of the operation.
  88. */
  89. int marimba_ssbi_write(struct marimba *marimba, u16 reg , u8 *value, int len)
  90. {
  91. struct i2c_msg *msg;
  92. int ret;
  93. marimba = &marimba_modules[marimba->mod_id];
  94. mutex_lock(&marimba->xfer_lock);
  95. msg = &marimba->xfer_msg[0];
  96. msg->addr = reg;
  97. msg->flags = 0x0;
  98. msg->buf = value;
  99. msg->len = len;
  100. ret = i2c_transfer(marimba->client->adapter, marimba->xfer_msg, 1);
  101. mutex_unlock(&marimba->xfer_lock);
  102. return ret;
  103. }
  104. EXPORT_SYMBOL(marimba_ssbi_write);
  105. /**
  106. * marimba_ssbi_read - Reads a n bit TSADC register in Marimba
  107. * @param marimba: marimba structure pointer passed by client
  108. * @param reg: register address
  109. * @param value: ssbi read of the register to be stored
  110. * @param len: num of bytes
  111. *
  112. * @returns result of the operation.
  113. */
  114. int marimba_ssbi_read(struct marimba *marimba, u16 reg, u8 *value, int len)
  115. {
  116. struct i2c_msg *msg;
  117. int ret;
  118. marimba = &marimba_modules[marimba->mod_id];
  119. mutex_lock(&marimba->xfer_lock);
  120. msg = &marimba->xfer_msg[0];
  121. msg->addr = reg;
  122. msg->flags = I2C_M_RD;
  123. msg->buf = value;
  124. msg->len = len;
  125. ret = i2c_transfer(marimba->client->adapter, marimba->xfer_msg, 1);
  126. mutex_unlock(&marimba->xfer_lock);
  127. return ret;
  128. }
  129. EXPORT_SYMBOL(marimba_ssbi_read);
  130. /**
  131. * marimba_write_bit_mask - Sets n bit register using bit mask
  132. * @param marimba: marimba structure pointer passed by client
  133. * @param reg: register address
  134. * @param value: buffer to be written to the registers
  135. * @param num_bytes: n bytes to write
  136. * @param mask: bit mask corresponding to the registers
  137. *
  138. * @returns result of the operation.
  139. */
  140. int marimba_write_bit_mask(struct marimba *marimba, u8 reg, u8 *value,
  141. unsigned num_bytes, u8 mask)
  142. {
  143. int ret, i;
  144. struct i2c_msg *msg;
  145. u8 data[num_bytes + 1];
  146. u8 mask_value[num_bytes];
  147. memset(mask_value, 0, sizeof(mask_value));
  148. marimba = &marimba_modules[marimba->mod_id];
  149. if (marimba == NULL) {
  150. pr_err("%s: Unable to access Marimba core\n", __func__);
  151. return -ENODEV;
  152. }
  153. mutex_lock(&marimba->xfer_lock);
  154. for (i = 0; i < num_bytes; i++)
  155. mask_value[i] = (marimba_shadow[marimba->mod_id][reg + i]
  156. & ~mask) | (value[i] & mask);
  157. msg = &marimba->xfer_msg[0];
  158. if (marimba->client == NULL) {
  159. pr_err("%s: Unable to access the Marimba slave device.\n",
  160. __func__);
  161. return -ENODEV;
  162. }
  163. msg->addr = marimba->client->addr;
  164. msg->flags = 0;
  165. msg->len = num_bytes + 1;
  166. msg->buf = data;
  167. data[0] = reg;
  168. memcpy(data+1, mask_value, num_bytes);
  169. ret = i2c_transfer(marimba->client->adapter, marimba->xfer_msg, 1);
  170. /* Try again if the write fails */
  171. if (ret != 1)
  172. ret = i2c_transfer(marimba->client->adapter,
  173. marimba->xfer_msg, 1);
  174. if (ret == 1) {
  175. for (i = 0; i < num_bytes; i++)
  176. marimba_shadow[marimba->mod_id][reg + i]
  177. = mask_value[i];
  178. } else {
  179. dev_err(&marimba->client->dev, "i2c write failed\n");
  180. ret = -ENODEV;
  181. }
  182. mutex_unlock(&marimba->xfer_lock);
  183. return ret;
  184. }
  185. EXPORT_SYMBOL(marimba_write_bit_mask);
  186. /**
  187. * marimba_write - Sets n bit register in Marimba
  188. * @param marimba: marimba structure pointer passed by client
  189. * @param reg: register address
  190. * @param value: buffer values to be written
  191. * @param num_bytes: n bytes to write
  192. *
  193. * @returns result of the operation.
  194. */
  195. int marimba_write(struct marimba *marimba, u8 reg, u8 *value,
  196. unsigned num_bytes)
  197. {
  198. return marimba_write_bit_mask(marimba, reg, value, num_bytes, 0xff);
  199. }
  200. EXPORT_SYMBOL(marimba_write);
  201. /**
  202. * marimba_read_bit_mask - Reads a n bit register based on bit mask
  203. * @param marimba: marimba structure pointer passed by client
  204. * @param reg: register address
  205. * @param value: i2c read of the register to be stored
  206. * @param num_bytes: n bytes to be read.
  207. * @param mask: bit mask concerning its register
  208. *
  209. * @returns result of the operation.
  210. */
  211. int marimba_read_bit_mask(struct marimba *marimba, u8 reg, u8 *value,
  212. unsigned num_bytes, u8 mask)
  213. {
  214. int ret, i;
  215. struct i2c_msg *msg;
  216. marimba = &marimba_modules[marimba->mod_id];
  217. mutex_lock(&marimba->xfer_lock);
  218. msg = &marimba->xfer_msg[0];
  219. msg->addr = marimba->client->addr;
  220. msg->len = 1;
  221. msg->flags = 0;
  222. msg->buf = &reg;
  223. msg = &marimba->xfer_msg[1];
  224. msg->addr = marimba->client->addr;
  225. msg->len = num_bytes;
  226. msg->flags = I2C_M_RD;
  227. msg->buf = value;
  228. ret = i2c_transfer(marimba->client->adapter, marimba->xfer_msg, 2);
  229. /* Try again if read fails first time */
  230. if (ret != 2)
  231. ret = i2c_transfer(marimba->client->adapter,
  232. marimba->xfer_msg, 2);
  233. if (ret == 2) {
  234. for (i = 0; i < num_bytes; i++) {
  235. marimba_shadow[marimba->mod_id][reg + i] = value[i];
  236. value[i] &= mask;
  237. }
  238. } else {
  239. dev_err(&marimba->client->dev, "i2c read failed\n");
  240. ret = -ENODEV;
  241. }
  242. mutex_unlock(&marimba->xfer_lock);
  243. return ret;
  244. }
  245. EXPORT_SYMBOL(marimba_read_bit_mask);
  246. /**
  247. * marimba_read - Reads n bit registers in Marimba
  248. * @param marimba: marimba structure pointer passed by client
  249. * @param reg: register address
  250. * @param value: i2c read of the register to be stored
  251. * @param num_bytes: n bytes to read.
  252. * @param mask: bit mask concerning its register
  253. *
  254. * @returns result of the operation.
  255. */
  256. int marimba_read(struct marimba *marimba, u8 reg, u8 *value, unsigned num_bytes)
  257. {
  258. return marimba_read_bit_mask(marimba, reg, value, num_bytes, 0xff);
  259. }
  260. EXPORT_SYMBOL(marimba_read);
  261. int timpani_read(struct marimba *marimba, u8 reg, u8 *value, unsigned num_bytes)
  262. {
  263. return marimba_read_bit_mask(marimba, reg, value, num_bytes, 0xff);
  264. }
  265. EXPORT_SYMBOL(timpani_read);
  266. int timpani_write(struct marimba *marimba, u8 reg,
  267. u8 *value, unsigned num_bytes)
  268. {
  269. return marimba_write_bit_mask(marimba, reg, value, num_bytes, 0xff);
  270. }
  271. EXPORT_SYMBOL(timpani_write);
  272. static int cur_codec_type = -1, cur_adie_type = -1, cur_connv_type = -1;
  273. static int adie_arry_idx;
  274. int adie_get_detected_codec_type(void)
  275. {
  276. return cur_codec_type;
  277. }
  278. EXPORT_SYMBOL(adie_get_detected_codec_type);
  279. int adie_get_detected_connectivity_type(void)
  280. {
  281. return cur_connv_type;
  282. }
  283. EXPORT_SYMBOL(adie_get_detected_connectivity_type);
  284. static struct device *
  285. add_numbered_child(unsigned chip, const char *name, int num, u8 driver_data,
  286. void *pdata, unsigned pdata_len)
  287. {
  288. struct platform_device *pdev;
  289. struct marimba *marimba = &marimba_modules[chip + adie_arry_idx];
  290. int status = 0;
  291. pdev = platform_device_alloc(name, num);
  292. if (!pdev) {
  293. status = -ENOMEM;
  294. return ERR_PTR(status);
  295. }
  296. pdev->dev.parent = &marimba->client->dev;
  297. marimba->mod_id = chip + adie_arry_idx;
  298. platform_set_drvdata(pdev, marimba);
  299. if (pdata) {
  300. status = platform_device_add_data(pdev, pdata, pdata_len);
  301. if (status < 0)
  302. goto err;
  303. }
  304. status = platform_device_add(pdev);
  305. if (status < 0)
  306. goto err;
  307. err:
  308. if (status < 0) {
  309. platform_set_drvdata(pdev, NULL);
  310. platform_device_put(pdev);
  311. dev_err(&marimba->client->dev, "can't add %s dev\n", name);
  312. return ERR_PTR(status);
  313. }
  314. return &pdev->dev;
  315. }
  316. static inline struct device *add_child(unsigned chip, const char *name,
  317. u8 driver_data, void *pdata, unsigned pdata_len)
  318. {
  319. return add_numbered_child(chip, name, -1, driver_data, pdata,
  320. pdata_len);
  321. }
  322. static int marimba_add_child(struct marimba_platform_data *pdata,
  323. u8 driver_data)
  324. {
  325. struct device *child;
  326. if (cur_adie_type == MARIMBA_ID) {
  327. child = add_child(MARIMBA_SLAVE_ID_FM, "marimba_fm",
  328. driver_data, pdata->fm, sizeof(*pdata->fm));
  329. if (IS_ERR(child))
  330. return PTR_ERR(child);
  331. } else if ((cur_adie_type == BAHAMA_ID) &&
  332. (cur_connv_type == BAHAMA_ID)) {
  333. child = add_child(BAHAMA_SLAVE_ID_FM_ID, "marimba_fm",
  334. driver_data, pdata->fm, sizeof(*pdata->fm));
  335. if (IS_ERR(child))
  336. return PTR_ERR(child);
  337. }
  338. /* Add Codec for Marimba and Timpani */
  339. if (cur_adie_type == MARIMBA_ID) {
  340. child = add_child(MARIMBA_SLAVE_ID_CDC, "marimba_codec",
  341. driver_data, pdata->codec, sizeof(*pdata->codec));
  342. if (IS_ERR(child))
  343. return PTR_ERR(child);
  344. } else if (cur_adie_type == TIMPANI_ID) {
  345. child = add_child(MARIMBA_SLAVE_ID_CDC, "timpani_codec",
  346. driver_data, pdata->codec, sizeof(*pdata->codec));
  347. if (IS_ERR(child))
  348. return PTR_ERR(child);
  349. }
  350. #if defined(CONFIG_I2C_SSBI)
  351. if ((pdata->tsadc != NULL) && (cur_adie_type != BAHAMA_ID)) {
  352. child = add_child(MARIMBA_ID_TSADC, "marimba_tsadc",
  353. driver_data, pdata->tsadc, sizeof(*pdata->tsadc));
  354. if (IS_ERR(child))
  355. return PTR_ERR(child);
  356. }
  357. #endif
  358. return 0;
  359. }
  360. int marimba_gpio_config(int gpio_value)
  361. {
  362. struct marimba *marimba = &marimba_modules[MARIMBA_SLAVE_ID_MARIMBA];
  363. struct marimba_platform_data *pdata = marimba_pdata;
  364. int rc = 0;
  365. /* Clients BT/FM need to manage GPIO 34 on Fusion for its clocks */
  366. mutex_lock(&marimba->xfer_lock);
  367. if (gpio_value) {
  368. marimba_gpio_count++;
  369. if (marimba_gpio_count == 1)
  370. rc = pdata->marimba_gpio_config(1);
  371. } else {
  372. marimba_gpio_count--;
  373. if (marimba_gpio_count == 0)
  374. rc = pdata->marimba_gpio_config(0);
  375. }
  376. mutex_unlock(&marimba->xfer_lock);
  377. return rc;
  378. }
  379. EXPORT_SYMBOL(marimba_gpio_config);
  380. bool marimba_get_fm_status(struct marimba *marimba)
  381. {
  382. bool ret;
  383. marimba = &marimba_modules[marimba->mod_id];
  384. mutex_lock(&marimba->xfer_lock);
  385. ret = fm_status;
  386. mutex_unlock(&marimba->xfer_lock);
  387. return ret;
  388. }
  389. EXPORT_SYMBOL(marimba_get_fm_status);
  390. void marimba_set_fm_status(struct marimba *marimba, bool value)
  391. {
  392. marimba = &marimba_modules[marimba->mod_id];
  393. mutex_lock(&marimba->xfer_lock);
  394. fm_status = value;
  395. mutex_unlock(&marimba->xfer_lock);
  396. }
  397. EXPORT_SYMBOL(marimba_set_fm_status);
  398. bool marimba_get_bt_status(struct marimba *marimba)
  399. {
  400. bool ret;
  401. marimba = &marimba_modules[marimba->mod_id];
  402. mutex_lock(&marimba->xfer_lock);
  403. ret = bt_status;
  404. mutex_unlock(&marimba->xfer_lock);
  405. return ret;
  406. }
  407. EXPORT_SYMBOL(marimba_get_bt_status);
  408. void marimba_set_bt_status(struct marimba *marimba, bool value)
  409. {
  410. marimba = &marimba_modules[marimba->mod_id];
  411. mutex_lock(&marimba->xfer_lock);
  412. bt_status = value;
  413. mutex_unlock(&marimba->xfer_lock);
  414. }
  415. EXPORT_SYMBOL(marimba_set_bt_status);
  416. #if defined(CONFIG_DEBUG_FS)
  417. static int check_addr(int addr, const char *func_name)
  418. {
  419. if (addr < 0 || addr > 0xFF) {
  420. pr_err("%s: Marimba register address is invalid: %d\n",
  421. func_name, addr);
  422. return -EINVAL;
  423. }
  424. return 0;
  425. }
  426. static int marimba_debugfs_set(void *data, u64 val)
  427. {
  428. struct adie_dbg_device *dbgdev = data;
  429. u8 reg = val;
  430. int rc;
  431. struct marimba marimba_id;
  432. mutex_lock(&dbgdev->dbg_mutex);
  433. rc = check_addr(dbgdev->addr, __func__);
  434. if (rc)
  435. goto done;
  436. marimba_id.mod_id = dbgdev->mod_id;
  437. rc = marimba_write(&marimba_id, dbgdev->addr, &reg, 1);
  438. rc = (rc == 1) ? 0 : rc;
  439. if (rc)
  440. pr_err("%s: FAIL marimba_write(0x%03X)=0x%02X: rc=%d\n",
  441. __func__, dbgdev->addr, reg, rc);
  442. done:
  443. mutex_unlock(&dbgdev->dbg_mutex);
  444. return rc;
  445. }
  446. static int marimba_debugfs_get(void *data, u64 *val)
  447. {
  448. struct adie_dbg_device *dbgdev = data;
  449. int rc;
  450. u8 reg;
  451. struct marimba marimba_id;
  452. mutex_lock(&dbgdev->dbg_mutex);
  453. rc = check_addr(dbgdev->addr, __func__);
  454. if (rc)
  455. goto done;
  456. marimba_id.mod_id = dbgdev->mod_id;
  457. rc = marimba_read(&marimba_id, dbgdev->addr, &reg, 1);
  458. rc = (rc == 2) ? 0 : rc;
  459. if (rc) {
  460. pr_err("%s: FAIL marimba_read(0x%03X)=0x%02X: rc=%d\n",
  461. __func__, dbgdev->addr, reg, rc);
  462. goto done;
  463. }
  464. *val = reg;
  465. done:
  466. mutex_unlock(&dbgdev->dbg_mutex);
  467. return rc;
  468. }
  469. DEFINE_SIMPLE_ATTRIBUTE(dbg_marimba_fops, marimba_debugfs_get,
  470. marimba_debugfs_set, "0x%02llX\n");
  471. static int addr_set(void *data, u64 val)
  472. {
  473. struct adie_dbg_device *dbgdev = data;
  474. int rc;
  475. rc = check_addr(val, __func__);
  476. if (rc)
  477. return rc;
  478. mutex_lock(&dbgdev->dbg_mutex);
  479. dbgdev->addr = val;
  480. mutex_unlock(&dbgdev->dbg_mutex);
  481. return 0;
  482. }
  483. static int addr_get(void *data, u64 *val)
  484. {
  485. struct adie_dbg_device *dbgdev = data;
  486. int rc;
  487. mutex_lock(&dbgdev->dbg_mutex);
  488. rc = check_addr(dbgdev->addr, __func__);
  489. if (rc) {
  490. mutex_unlock(&dbgdev->dbg_mutex);
  491. return rc;
  492. }
  493. *val = dbgdev->addr;
  494. mutex_unlock(&dbgdev->dbg_mutex);
  495. return 0;
  496. }
  497. DEFINE_SIMPLE_ATTRIBUTE(dbg_addr_fops, addr_get, addr_set, "0x%03llX\n");
  498. static int __devinit marimba_dbg_init(int adie_type)
  499. {
  500. struct adie_dbg_device *dbgdev;
  501. struct dentry *dent = NULL;
  502. struct dentry *temp;
  503. dbgdev = kzalloc(sizeof *dbgdev, GFP_KERNEL);
  504. if (dbgdev == NULL) {
  505. pr_err("%s: kzalloc() failed.\n", __func__);
  506. return -ENOMEM;
  507. }
  508. mutex_init(&dbgdev->dbg_mutex);
  509. dbgdev->addr = -1;
  510. if (adie_type == MARIMBA_ID) {
  511. marimba_dbg_device = dbgdev;
  512. marimba_dbg_device->mod_id = MARIMBA_SLAVE_ID_MARIMBA;
  513. dent = debugfs_create_dir("marimba-dbg", NULL);
  514. } else if (adie_type == TIMPANI_ID) {
  515. timpani_dbg_device = dbgdev;
  516. timpani_dbg_device->mod_id = MARIMBA_SLAVE_ID_MARIMBA;
  517. dent = debugfs_create_dir("timpani-dbg", NULL);
  518. } else if (adie_type == BAHAMA_ID) {
  519. bahama_dbg_device = dbgdev;
  520. bahama_dbg_device->mod_id = SLAVE_ID_BAHAMA;
  521. dent = debugfs_create_dir("bahama-dbg", NULL);
  522. }
  523. if (dent == NULL || IS_ERR(dent)) {
  524. pr_err("%s: ERR debugfs_create_dir: dent=0x%X\n",
  525. __func__, (unsigned)dent);
  526. kfree(dbgdev);
  527. return -ENOMEM;
  528. }
  529. temp = debugfs_create_file("addr", S_IRUSR | S_IWUSR, dent,
  530. dbgdev, &dbg_addr_fops);
  531. if (temp == NULL || IS_ERR(temp)) {
  532. pr_err("%s: ERR debugfs_create_file: dent=0x%X\n",
  533. __func__, (unsigned)temp);
  534. goto debug_error;
  535. }
  536. temp = debugfs_create_file("data", S_IRUSR | S_IWUSR, dent,
  537. dbgdev, &dbg_marimba_fops);
  538. if (temp == NULL || IS_ERR(temp)) {
  539. pr_err("%s: ERR debugfs_create_file: dent=0x%X\n",
  540. __func__, (unsigned)temp);
  541. goto debug_error;
  542. }
  543. dbgdev->dent = dent;
  544. return 0;
  545. debug_error:
  546. kfree(dbgdev);
  547. debugfs_remove_recursive(dent);
  548. return -ENOMEM;
  549. }
  550. static int __devexit marimba_dbg_remove(void)
  551. {
  552. if (marimba_dbg_device) {
  553. debugfs_remove_recursive(marimba_dbg_device->dent);
  554. kfree(marimba_dbg_device);
  555. }
  556. if (timpani_dbg_device) {
  557. debugfs_remove_recursive(timpani_dbg_device->dent);
  558. kfree(timpani_dbg_device);
  559. }
  560. if (bahama_dbg_device) {
  561. debugfs_remove_recursive(bahama_dbg_device->dent);
  562. kfree(bahama_dbg_device);
  563. }
  564. return 0;
  565. }
  566. #else
  567. static int __devinit marimba_dbg_init(int adie_type)
  568. {
  569. return 0;
  570. }
  571. static int __devexit marimba_dbg_remove(void)
  572. {
  573. return 0;
  574. }
  575. #endif
  576. static int get_adie_type(void)
  577. {
  578. u8 rd_val;
  579. int ret;
  580. struct marimba *marimba = &marimba_modules[ADIE_ARRY_SIZE - 1];
  581. marimba->mod_id = ADIE_ARRY_SIZE - 1;
  582. /* Enable the Mode for Marimba/Timpani */
  583. ret = marimba_read(marimba, MARIMBA_MODE_REG, &rd_val, 1);
  584. if (ret >= 0) {
  585. if (rd_val & 0x80) {
  586. cur_adie_type = BAHAMA_ID;
  587. return cur_adie_type;
  588. } else {
  589. ret = marimba_read(marimba,
  590. MARIMBA_VERSION_REG, &rd_val, 1);
  591. if ((ret >= 0) && (rd_val & 0x20)) {
  592. cur_adie_type = TIMPANI_ID;
  593. return cur_adie_type;
  594. } else if (ret >= 0) {
  595. cur_adie_type = MARIMBA_ID;
  596. return cur_adie_type;
  597. }
  598. }
  599. }
  600. return ret;
  601. }
  602. static void marimba_init_reg(struct i2c_client *client, u8 driver_data)
  603. {
  604. struct marimba_platform_data *pdata = client->dev.platform_data;
  605. struct marimba *marimba =
  606. &marimba_modules[MARIMBA_SLAVE_ID_MARIMBA + adie_arry_idx];
  607. u8 buf[1];
  608. buf[0] = 0x10;
  609. if (cur_adie_type != BAHAMA_ID) {
  610. marimba->mod_id = MARIMBA_SLAVE_ID_MARIMBA + adie_arry_idx;
  611. /* Enable the Mode for Marimba/Timpani */
  612. marimba_write(marimba, MARIMBA_MODE, buf, 1);
  613. } else if ((cur_adie_type == BAHAMA_ID) &&
  614. (cur_connv_type == BAHAMA_ID)) {
  615. marimba->mod_id = MARIMBA_SLAVE_ID_MARIMBA + adie_arry_idx;
  616. marimba_write(marimba, BAHAMA_SLAVE_ID_FM_ID,
  617. &pdata->slave_id[SLAVE_ID_BAHAMA_FM], 1);
  618. /* Configure Bahama core registers (AREG & DREG) */
  619. /* with optimal values to eliminate power leakage */
  620. if (pdata->bahama_core_config != NULL)
  621. pdata->bahama_core_config(cur_adie_type);
  622. }
  623. }
  624. static int __devinit marimba_probe(struct i2c_client *client,
  625. const struct i2c_device_id *id)
  626. {
  627. struct marimba_platform_data *pdata = client->dev.platform_data;
  628. struct i2c_adapter *ssbi_adap;
  629. struct marimba *marimba;
  630. int i, status, rc, client_loop, adie_slave_idx_offset;
  631. int rc_bahama = 0, rc_marimba = 0;
  632. if (!pdata) {
  633. dev_dbg(&client->dev, "no platform data?\n");
  634. status = -EINVAL;
  635. goto fail;
  636. }
  637. if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C) == 0) {
  638. dev_dbg(&client->dev, "can't talk I2C?\n");
  639. status = -EIO;
  640. goto fail;
  641. }
  642. if (!mutex_initialized) {
  643. for (i = 0; i < ADIE_ARRY_SIZE; ++i) {
  644. marimba = &marimba_modules[i];
  645. mutex_init(&marimba->xfer_lock);
  646. }
  647. mutex_initialized = 1;
  648. }
  649. /* First, identify the codec type */
  650. if (pdata->marimba_setup != NULL) {
  651. rc_marimba = pdata->marimba_setup();
  652. if (rc_marimba)
  653. pdata->marimba_shutdown();
  654. }
  655. if (pdata->bahama_setup != NULL &&
  656. cur_connv_type != BAHAMA_ID) {
  657. rc_bahama = pdata->bahama_setup();
  658. if (rc_bahama)
  659. pdata->bahama_shutdown(cur_connv_type);
  660. }
  661. if (rc_marimba & rc_bahama) {
  662. status = -EAGAIN;
  663. goto fail;
  664. }
  665. marimba = &marimba_modules[ADIE_ARRY_SIZE - 1];
  666. marimba->client = client;
  667. rc = get_adie_type();
  668. if (rc < 0) {
  669. if (pdata->bahama_setup != NULL)
  670. pdata->bahama_shutdown(cur_adie_type);
  671. if (pdata->marimba_shutdown != NULL)
  672. pdata->marimba_shutdown();
  673. status = -ENODEV;
  674. goto fail;
  675. }
  676. if (rc < 2) {
  677. adie_arry_idx = 0;
  678. adie_slave_idx_offset = 0;
  679. client_loop = 0;
  680. cur_codec_type = rc;
  681. if (cur_connv_type < 0)
  682. cur_connv_type = rc;
  683. if (pdata->bahama_shutdown != NULL)
  684. pdata->bahama_shutdown(cur_connv_type);
  685. } else {
  686. adie_arry_idx = 5;
  687. adie_slave_idx_offset = 5;
  688. client_loop = 1;
  689. cur_connv_type = rc;
  690. }
  691. marimba = &marimba_modules[adie_arry_idx];
  692. marimba->client = client;
  693. for (i = 1; i <= (NUM_ADD - client_loop); i++) {
  694. /* Skip adding BT/FM for Timpani */
  695. if (i == 1 && rc >= 1)
  696. i++;
  697. marimba = &marimba_modules[i + adie_arry_idx];
  698. if (i != MARIMBA_ID_TSADC)
  699. marimba->client = i2c_new_dummy(client->adapter,
  700. pdata->slave_id[i + adie_slave_idx_offset]);
  701. else if (pdata->tsadc_ssbi_adap) {
  702. ssbi_adap = i2c_get_adapter(pdata->tsadc_ssbi_adap);
  703. marimba->client = i2c_new_dummy(ssbi_adap,
  704. 0x55);
  705. } else
  706. ssbi_adap = NULL;
  707. if (!marimba->client) {
  708. pr_err("can't attach client %d\n", i);
  709. status = -ENOMEM;
  710. goto fail;
  711. }
  712. strlcpy(marimba->client->name, id->name,
  713. sizeof(marimba->client->name));
  714. }
  715. if (marimba_dbg_init(rc) != 0)
  716. pr_debug("%s: marimba debugfs init failed\n", __func__);
  717. marimba_init_reg(client, id->driver_data);
  718. status = marimba_add_child(pdata, id->driver_data);
  719. marimba_pdata = pdata;
  720. return 0;
  721. fail:
  722. return status;
  723. }
  724. static int __devexit marimba_remove(struct i2c_client *client)
  725. {
  726. int i;
  727. struct marimba_platform_data *pdata;
  728. pdata = client->dev.platform_data;
  729. for (i = 0; i < ADIE_ARRY_SIZE; i++) {
  730. struct marimba *marimba = &marimba_modules[i];
  731. if (marimba->client && marimba->client != client)
  732. i2c_unregister_device(marimba->client);
  733. marimba_modules[i].client = NULL;
  734. if (mutex_initialized)
  735. mutex_destroy(&marimba->xfer_lock);
  736. }
  737. marimba_dbg_remove();
  738. mutex_initialized = 0;
  739. if (pdata->marimba_shutdown != NULL)
  740. pdata->marimba_shutdown();
  741. return 0;
  742. }
  743. static struct i2c_device_id marimba_id_table[] = {
  744. {"marimba", MARIMBA_ID},
  745. {"timpani", TIMPANI_ID},
  746. {}
  747. };
  748. MODULE_DEVICE_TABLE(i2c, marimba_id_table);
  749. static struct i2c_driver marimba_driver = {
  750. .driver = {
  751. .owner = THIS_MODULE,
  752. .name = "marimba-core",
  753. },
  754. .id_table = marimba_id_table,
  755. .probe = marimba_probe,
  756. .remove = __devexit_p(marimba_remove),
  757. };
  758. static int __init marimba_init(void)
  759. {
  760. return i2c_add_driver(&marimba_driver);
  761. }
  762. module_init(marimba_init);
  763. static void __exit marimba_exit(void)
  764. {
  765. i2c_del_driver(&marimba_driver);
  766. }
  767. module_exit(marimba_exit);
  768. MODULE_DESCRIPTION("Marimba Top level Driver");
  769. MODULE_ALIAS("platform:marimba-core");
  770. MODULE_LICENSE("GPL v2");
  771. MODULE_VERSION("0.1");