cx18-i2c.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * cx18 I2C functions
  3. *
  4. * Derived from ivtv-i2c.c
  5. *
  6. * Copyright (C) 2007 Hans Verkuil <hverkuil@xs4all.nl>
  7. * Copyright (C) 2008 Andy Walls <awalls@radix.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  22. * 02111-1307 USA
  23. */
  24. #include "cx18-driver.h"
  25. #include "cx18-io.h"
  26. #include "cx18-cards.h"
  27. #include "cx18-gpio.h"
  28. #include "cx18-av-core.h"
  29. #include "cx18-i2c.h"
  30. #include "cx18-irq.h"
  31. #define CX18_REG_I2C_1_WR 0xf15000
  32. #define CX18_REG_I2C_1_RD 0xf15008
  33. #define CX18_REG_I2C_2_WR 0xf25100
  34. #define CX18_REG_I2C_2_RD 0xf25108
  35. #define SETSCL_BIT 0x0001
  36. #define SETSDL_BIT 0x0002
  37. #define GETSCL_BIT 0x0004
  38. #define GETSDL_BIT 0x0008
  39. #define CX18_CS5345_I2C_ADDR 0x4c
  40. /* This array should match the CX18_HW_ defines */
  41. static const u8 hw_driverids[] = {
  42. I2C_DRIVERID_TUNER,
  43. I2C_DRIVERID_TVEEPROM,
  44. I2C_DRIVERID_CS5345,
  45. 0, /* CX18_HW_GPIO dummy driver ID */
  46. 0 /* CX18_HW_CX23418 dummy driver ID */
  47. };
  48. /* This array should match the CX18_HW_ defines */
  49. static const u8 hw_addrs[] = {
  50. 0,
  51. 0,
  52. CX18_CS5345_I2C_ADDR,
  53. 0, /* CX18_HW_GPIO dummy driver ID */
  54. 0, /* CX18_HW_CX23418 dummy driver ID */
  55. };
  56. /* This array should match the CX18_HW_ defines */
  57. /* This might well become a card-specific array */
  58. static const u8 hw_bus[] = {
  59. 0,
  60. 0,
  61. 0,
  62. 0, /* CX18_HW_GPIO dummy driver ID */
  63. 0, /* CX18_HW_CX23418 dummy driver ID */
  64. };
  65. /* This array should match the CX18_HW_ defines */
  66. static const char * const hw_devicenames[] = {
  67. "tuner",
  68. "tveeprom",
  69. "cs5345",
  70. "gpio",
  71. "cx23418",
  72. };
  73. int cx18_i2c_register(struct cx18 *cx, unsigned idx)
  74. {
  75. struct i2c_board_info info;
  76. struct i2c_client *c;
  77. u8 id, bus;
  78. int i;
  79. CX18_DEBUG_I2C("i2c client register\n");
  80. if (idx >= ARRAY_SIZE(hw_driverids) || hw_driverids[idx] == 0)
  81. return -1;
  82. id = hw_driverids[idx];
  83. bus = hw_bus[idx];
  84. memset(&info, 0, sizeof(info));
  85. strlcpy(info.type, hw_devicenames[idx], sizeof(info.type));
  86. info.addr = hw_addrs[idx];
  87. for (i = 0; i < I2C_CLIENTS_MAX; i++)
  88. if (cx->i2c_clients[i] == NULL)
  89. break;
  90. if (i == I2C_CLIENTS_MAX) {
  91. CX18_ERR("insufficient room for new I2C client!\n");
  92. return -ENOMEM;
  93. }
  94. if (id != I2C_DRIVERID_TUNER) {
  95. c = i2c_new_device(&cx->i2c_adap[bus], &info);
  96. if (c->driver == NULL)
  97. i2c_unregister_device(c);
  98. else
  99. cx->i2c_clients[i] = c;
  100. return cx->i2c_clients[i] ? 0 : -ENODEV;
  101. }
  102. /* special tuner handling */
  103. c = i2c_new_probed_device(&cx->i2c_adap[1], &info, cx->card_i2c->radio);
  104. if (c && c->driver == NULL)
  105. i2c_unregister_device(c);
  106. else if (c)
  107. cx->i2c_clients[i++] = c;
  108. c = i2c_new_probed_device(&cx->i2c_adap[1], &info, cx->card_i2c->demod);
  109. if (c && c->driver == NULL)
  110. i2c_unregister_device(c);
  111. else if (c)
  112. cx->i2c_clients[i++] = c;
  113. c = i2c_new_probed_device(&cx->i2c_adap[1], &info, cx->card_i2c->tv);
  114. if (c && c->driver == NULL)
  115. i2c_unregister_device(c);
  116. else if (c)
  117. cx->i2c_clients[i++] = c;
  118. return 0;
  119. }
  120. static int attach_inform(struct i2c_client *client)
  121. {
  122. return 0;
  123. }
  124. static int detach_inform(struct i2c_client *client)
  125. {
  126. int i;
  127. struct cx18 *cx = (struct cx18 *)i2c_get_adapdata(client->adapter);
  128. CX18_DEBUG_I2C("i2c client detach\n");
  129. for (i = 0; i < I2C_CLIENTS_MAX; i++) {
  130. if (cx->i2c_clients[i] == client) {
  131. cx->i2c_clients[i] = NULL;
  132. break;
  133. }
  134. }
  135. CX18_DEBUG_I2C("i2c detach [client=%s,%s]\n",
  136. client->name, (i < I2C_CLIENTS_MAX) ? "ok" : "failed");
  137. return 0;
  138. }
  139. static void cx18_setscl(void *data, int state)
  140. {
  141. struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
  142. int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
  143. u32 addr = bus_index ? CX18_REG_I2C_2_WR : CX18_REG_I2C_1_WR;
  144. u32 r = cx18_read_reg(cx, addr);
  145. if (state)
  146. cx18_write_reg(cx, r | SETSCL_BIT, addr);
  147. else
  148. cx18_write_reg(cx, r & ~SETSCL_BIT, addr);
  149. }
  150. static void cx18_setsda(void *data, int state)
  151. {
  152. struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
  153. int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
  154. u32 addr = bus_index ? CX18_REG_I2C_2_WR : CX18_REG_I2C_1_WR;
  155. u32 r = cx18_read_reg(cx, addr);
  156. if (state)
  157. cx18_write_reg(cx, r | SETSDL_BIT, addr);
  158. else
  159. cx18_write_reg(cx, r & ~SETSDL_BIT, addr);
  160. }
  161. static int cx18_getscl(void *data)
  162. {
  163. struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
  164. int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
  165. u32 addr = bus_index ? CX18_REG_I2C_2_RD : CX18_REG_I2C_1_RD;
  166. return cx18_read_reg(cx, addr) & GETSCL_BIT;
  167. }
  168. static int cx18_getsda(void *data)
  169. {
  170. struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
  171. int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
  172. u32 addr = bus_index ? CX18_REG_I2C_2_RD : CX18_REG_I2C_1_RD;
  173. return cx18_read_reg(cx, addr) & GETSDL_BIT;
  174. }
  175. /* template for i2c-bit-algo */
  176. static struct i2c_adapter cx18_i2c_adap_template = {
  177. .name = "cx18 i2c driver",
  178. .id = I2C_HW_B_CX2341X,
  179. .algo = NULL, /* set by i2c-algo-bit */
  180. .algo_data = NULL, /* filled from template */
  181. .client_register = attach_inform,
  182. .client_unregister = detach_inform,
  183. .owner = THIS_MODULE,
  184. };
  185. #define CX18_SCL_PERIOD (10) /* usecs. 10 usec is period for a 100 KHz clock */
  186. #define CX18_ALGO_BIT_TIMEOUT (2) /* seconds */
  187. static struct i2c_algo_bit_data cx18_i2c_algo_template = {
  188. .setsda = cx18_setsda,
  189. .setscl = cx18_setscl,
  190. .getsda = cx18_getsda,
  191. .getscl = cx18_getscl,
  192. .udelay = CX18_SCL_PERIOD/2, /* 1/2 clock period in usec*/
  193. .timeout = CX18_ALGO_BIT_TIMEOUT*HZ /* jiffies */
  194. };
  195. static struct i2c_client cx18_i2c_client_template = {
  196. .name = "cx18 internal",
  197. };
  198. int cx18_call_i2c_client(struct cx18 *cx, int addr, unsigned cmd, void *arg)
  199. {
  200. struct i2c_client *client;
  201. int retval;
  202. int i;
  203. CX18_DEBUG_I2C("call_i2c_client addr=%02x\n", addr);
  204. for (i = 0; i < I2C_CLIENTS_MAX; i++) {
  205. client = cx->i2c_clients[i];
  206. if (client == NULL || client->driver == NULL ||
  207. client->driver->command == NULL)
  208. continue;
  209. if (addr == client->addr) {
  210. retval = client->driver->command(client, cmd, arg);
  211. return retval;
  212. }
  213. }
  214. if (cmd != VIDIOC_G_CHIP_IDENT)
  215. CX18_ERR("i2c addr 0x%02x not found for cmd 0x%x!\n",
  216. addr, cmd);
  217. return -ENODEV;
  218. }
  219. /* Find the i2c device based on the driver ID and return
  220. its i2c address or -ENODEV if no matching device was found. */
  221. static int cx18_i2c_id_addr(struct cx18 *cx, u32 id)
  222. {
  223. struct i2c_client *client;
  224. int retval = -ENODEV;
  225. int i;
  226. for (i = 0; i < I2C_CLIENTS_MAX; i++) {
  227. client = cx->i2c_clients[i];
  228. if (client == NULL || client->driver == NULL)
  229. continue;
  230. if (id == client->driver->id) {
  231. retval = client->addr;
  232. break;
  233. }
  234. }
  235. return retval;
  236. }
  237. /* Find the i2c device name matching the DRIVERID */
  238. static const char *cx18_i2c_id_name(u32 id)
  239. {
  240. int i;
  241. for (i = 0; i < ARRAY_SIZE(hw_driverids); i++)
  242. if (hw_driverids[i] == id)
  243. return hw_devicenames[i];
  244. return "unknown device";
  245. }
  246. /* Find the i2c device name matching the CX18_HW_ flag */
  247. static const char *cx18_i2c_hw_name(u32 hw)
  248. {
  249. int i;
  250. for (i = 0; i < ARRAY_SIZE(hw_driverids); i++)
  251. if (1 << i == hw)
  252. return hw_devicenames[i];
  253. return "unknown device";
  254. }
  255. /* Find the i2c device matching the CX18_HW_ flag and return
  256. its i2c address or -ENODEV if no matching device was found. */
  257. int cx18_i2c_hw_addr(struct cx18 *cx, u32 hw)
  258. {
  259. int i;
  260. for (i = 0; i < ARRAY_SIZE(hw_driverids); i++)
  261. if (1 << i == hw)
  262. return cx18_i2c_id_addr(cx, hw_driverids[i]);
  263. return -ENODEV;
  264. }
  265. /* Calls i2c device based on CX18_HW_ flag. If hw == 0, then do nothing.
  266. If hw == CX18_HW_GPIO then call the gpio handler. */
  267. int cx18_i2c_hw(struct cx18 *cx, u32 hw, unsigned int cmd, void *arg)
  268. {
  269. int addr;
  270. if (hw == 0)
  271. return 0;
  272. if (hw == CX18_HW_GPIO)
  273. return cx18_gpio(cx, cmd, arg);
  274. if (hw == CX18_HW_CX23418)
  275. return cx18_av_cmd(cx, cmd, arg);
  276. addr = cx18_i2c_hw_addr(cx, hw);
  277. if (addr < 0) {
  278. CX18_ERR("i2c hardware 0x%08x (%s) not found for cmd 0x%x!\n",
  279. hw, cx18_i2c_hw_name(hw), cmd);
  280. return addr;
  281. }
  282. return cx18_call_i2c_client(cx, addr, cmd, arg);
  283. }
  284. /* Calls i2c device based on I2C driver ID. */
  285. int cx18_i2c_id(struct cx18 *cx, u32 id, unsigned int cmd, void *arg)
  286. {
  287. int addr;
  288. addr = cx18_i2c_id_addr(cx, id);
  289. if (addr < 0) {
  290. if (cmd != VIDIOC_G_CHIP_IDENT)
  291. CX18_ERR("i2c ID 0x%08x (%s) not found for cmd 0x%x!\n",
  292. id, cx18_i2c_id_name(id), cmd);
  293. return addr;
  294. }
  295. return cx18_call_i2c_client(cx, addr, cmd, arg);
  296. }
  297. /* broadcast cmd for all I2C clients and for the gpio subsystem */
  298. void cx18_call_i2c_clients(struct cx18 *cx, unsigned int cmd, void *arg)
  299. {
  300. if (cx->i2c_adap[0].algo == NULL || cx->i2c_adap[1].algo == NULL) {
  301. CX18_ERR("adapter is not set\n");
  302. return;
  303. }
  304. cx18_av_cmd(cx, cmd, arg);
  305. i2c_clients_command(&cx->i2c_adap[0], cmd, arg);
  306. i2c_clients_command(&cx->i2c_adap[1], cmd, arg);
  307. if (cx->hw_flags & CX18_HW_GPIO)
  308. cx18_gpio(cx, cmd, arg);
  309. }
  310. /* init + register i2c algo-bit adapter */
  311. int init_cx18_i2c(struct cx18 *cx)
  312. {
  313. int i;
  314. CX18_DEBUG_I2C("i2c init\n");
  315. /* Sanity checks for the I2C hardware arrays. They must be the
  316. * same size and GPIO/CX23418 must be the last entries.
  317. */
  318. if (ARRAY_SIZE(hw_driverids) != ARRAY_SIZE(hw_addrs) ||
  319. ARRAY_SIZE(hw_devicenames) != ARRAY_SIZE(hw_addrs) ||
  320. CX18_HW_GPIO != (1 << (ARRAY_SIZE(hw_addrs) - 2)) ||
  321. CX18_HW_CX23418 != (1 << (ARRAY_SIZE(hw_addrs) - 1)) ||
  322. hw_driverids[ARRAY_SIZE(hw_addrs) - 1]) {
  323. CX18_ERR("Mismatched I2C hardware arrays\n");
  324. return -ENODEV;
  325. }
  326. for (i = 0; i < 2; i++) {
  327. memcpy(&cx->i2c_adap[i], &cx18_i2c_adap_template,
  328. sizeof(struct i2c_adapter));
  329. memcpy(&cx->i2c_algo[i], &cx18_i2c_algo_template,
  330. sizeof(struct i2c_algo_bit_data));
  331. cx->i2c_algo_cb_data[i].cx = cx;
  332. cx->i2c_algo_cb_data[i].bus_index = i;
  333. cx->i2c_algo[i].data = &cx->i2c_algo_cb_data[i];
  334. cx->i2c_adap[i].algo_data = &cx->i2c_algo[i];
  335. sprintf(cx->i2c_adap[i].name + strlen(cx->i2c_adap[i].name),
  336. " #%d-%d", cx->num, i);
  337. i2c_set_adapdata(&cx->i2c_adap[i], cx);
  338. memcpy(&cx->i2c_client[i], &cx18_i2c_client_template,
  339. sizeof(struct i2c_client));
  340. sprintf(cx->i2c_client[i].name +
  341. strlen(cx->i2c_client[i].name), "%d", i);
  342. cx->i2c_client[i].adapter = &cx->i2c_adap[i];
  343. cx->i2c_adap[i].dev.parent = &cx->dev->dev;
  344. }
  345. if (cx18_read_reg(cx, CX18_REG_I2C_2_WR) != 0x0003c02f) {
  346. /* Reset/Unreset I2C hardware block */
  347. /* Clock select 220MHz */
  348. cx18_write_reg_expect(cx, 0x10000000, 0xc71004,
  349. 0x00000000, 0x10001000);
  350. /* Clock Enable */
  351. cx18_write_reg_expect(cx, 0x10001000, 0xc71024,
  352. 0x00001000, 0x10001000);
  353. }
  354. /* courtesy of Steven Toth <stoth@hauppauge.com> */
  355. cx18_write_reg_expect(cx, 0x00c00000, 0xc7001c, 0x00000000, 0x00c000c0);
  356. mdelay(10);
  357. cx18_write_reg_expect(cx, 0x00c000c0, 0xc7001c, 0x000000c0, 0x00c000c0);
  358. mdelay(10);
  359. cx18_write_reg_expect(cx, 0x00c00000, 0xc7001c, 0x00000000, 0x00c000c0);
  360. mdelay(10);
  361. /* Set to edge-triggered intrs. */
  362. cx18_write_reg(cx, 0x00c00000, 0xc730c8);
  363. /* Clear any stale intrs */
  364. cx18_write_reg_expect(cx, HW2_I2C1_INT|HW2_I2C2_INT, HW2_INT_CLR_STATUS,
  365. ~(HW2_I2C1_INT|HW2_I2C2_INT), HW2_I2C1_INT|HW2_I2C2_INT);
  366. /* Hw I2C1 Clock Freq ~100kHz */
  367. cx18_write_reg(cx, 0x00021c0f & ~4, CX18_REG_I2C_1_WR);
  368. cx18_setscl(&cx->i2c_algo_cb_data[0], 1);
  369. cx18_setsda(&cx->i2c_algo_cb_data[0], 1);
  370. /* Hw I2C2 Clock Freq ~100kHz */
  371. cx18_write_reg(cx, 0x00021c0f & ~4, CX18_REG_I2C_2_WR);
  372. cx18_setscl(&cx->i2c_algo_cb_data[1], 1);
  373. cx18_setsda(&cx->i2c_algo_cb_data[1], 1);
  374. cx18_reset_i2c_slaves_gpio(cx);
  375. return i2c_bit_add_bus(&cx->i2c_adap[0]) ||
  376. i2c_bit_add_bus(&cx->i2c_adap[1]);
  377. }
  378. void exit_cx18_i2c(struct cx18 *cx)
  379. {
  380. int i;
  381. CX18_DEBUG_I2C("i2c exit\n");
  382. cx18_write_reg(cx, cx18_read_reg(cx, CX18_REG_I2C_1_WR) | 4,
  383. CX18_REG_I2C_1_WR);
  384. cx18_write_reg(cx, cx18_read_reg(cx, CX18_REG_I2C_2_WR) | 4,
  385. CX18_REG_I2C_2_WR);
  386. for (i = 0; i < 2; i++) {
  387. i2c_del_adapter(&cx->i2c_adap[i]);
  388. }
  389. }
  390. /*
  391. Hauppauge HVR1600 should have:
  392. 32 cx24227
  393. 98 unknown
  394. a0 eeprom
  395. c2 tuner
  396. e? zilog ir
  397. */