cx18-i2c.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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@md.metrocast.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. #include "cx18-driver.h"
  20. #include "cx18-io.h"
  21. #include "cx18-cards.h"
  22. #include "cx18-gpio.h"
  23. #include "cx18-i2c.h"
  24. #include "cx18-irq.h"
  25. #define CX18_REG_I2C_1_WR 0xf15000
  26. #define CX18_REG_I2C_1_RD 0xf15008
  27. #define CX18_REG_I2C_2_WR 0xf25100
  28. #define CX18_REG_I2C_2_RD 0xf25108
  29. #define SETSCL_BIT 0x0001
  30. #define SETSDL_BIT 0x0002
  31. #define GETSCL_BIT 0x0004
  32. #define GETSDL_BIT 0x0008
  33. #define CX18_CS5345_I2C_ADDR 0x4c
  34. #define CX18_Z8F0811_IR_TX_I2C_ADDR 0x70
  35. #define CX18_Z8F0811_IR_RX_I2C_ADDR 0x71
  36. /* This array should match the CX18_HW_ defines */
  37. static const u8 hw_addrs[] = {
  38. 0, /* CX18_HW_TUNER */
  39. 0, /* CX18_HW_TVEEPROM */
  40. CX18_CS5345_I2C_ADDR, /* CX18_HW_CS5345 */
  41. 0, /* CX18_HW_DVB */
  42. 0, /* CX18_HW_418_AV */
  43. 0, /* CX18_HW_GPIO_MUX */
  44. 0, /* CX18_HW_GPIO_RESET_CTRL */
  45. CX18_Z8F0811_IR_RX_I2C_ADDR, /* CX18_HW_Z8F0811_IR_HAUP */
  46. };
  47. /* This array should match the CX18_HW_ defines */
  48. /* This might well become a card-specific array */
  49. static const u8 hw_bus[] = {
  50. 1, /* CX18_HW_TUNER */
  51. 0, /* CX18_HW_TVEEPROM */
  52. 0, /* CX18_HW_CS5345 */
  53. 0, /* CX18_HW_DVB */
  54. 0, /* CX18_HW_418_AV */
  55. 0, /* CX18_HW_GPIO_MUX */
  56. 0, /* CX18_HW_GPIO_RESET_CTRL */
  57. 0, /* CX18_HW_Z8F0811_IR_HAUP */
  58. };
  59. /* This array should match the CX18_HW_ defines */
  60. static const char * const hw_devicenames[] = {
  61. "tuner",
  62. "tveeprom",
  63. "cs5345",
  64. "cx23418_DTV",
  65. "cx23418_AV",
  66. "gpio_mux",
  67. "gpio_reset_ctrl",
  68. "ir_z8f0811_haup",
  69. };
  70. static int cx18_i2c_new_ir(struct cx18 *cx, struct i2c_adapter *adap, u32 hw,
  71. const char *type, u8 addr)
  72. {
  73. struct i2c_board_info info;
  74. struct IR_i2c_init_data *init_data = &cx->ir_i2c_init_data;
  75. unsigned short addr_list[2] = { addr, I2C_CLIENT_END };
  76. memset(&info, 0, sizeof(struct i2c_board_info));
  77. strlcpy(info.type, type, I2C_NAME_SIZE);
  78. /* Our default information for ir-kbd-i2c.c to use */
  79. switch (hw) {
  80. case CX18_HW_Z8F0811_IR_HAUP:
  81. init_data->ir_codes = RC_MAP_HAUPPAUGE;
  82. init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
  83. init_data->type = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC6_MCE |
  84. RC_PROTO_BIT_RC6_6A_32;
  85. init_data->name = cx->card_name;
  86. info.platform_data = init_data;
  87. break;
  88. }
  89. return i2c_new_probed_device(adap, &info, addr_list, NULL) == NULL ?
  90. -1 : 0;
  91. }
  92. int cx18_i2c_register(struct cx18 *cx, unsigned idx)
  93. {
  94. struct v4l2_subdev *sd;
  95. int bus = hw_bus[idx];
  96. struct i2c_adapter *adap = &cx->i2c_adap[bus];
  97. const char *type = hw_devicenames[idx];
  98. u32 hw = 1 << idx;
  99. if (hw == CX18_HW_TUNER) {
  100. /* special tuner group handling */
  101. sd = v4l2_i2c_new_subdev(&cx->v4l2_dev,
  102. adap, type, 0, cx->card_i2c->radio);
  103. if (sd != NULL)
  104. sd->grp_id = hw;
  105. sd = v4l2_i2c_new_subdev(&cx->v4l2_dev,
  106. adap, type, 0, cx->card_i2c->demod);
  107. if (sd != NULL)
  108. sd->grp_id = hw;
  109. sd = v4l2_i2c_new_subdev(&cx->v4l2_dev,
  110. adap, type, 0, cx->card_i2c->tv);
  111. if (sd != NULL)
  112. sd->grp_id = hw;
  113. return sd != NULL ? 0 : -1;
  114. }
  115. if (hw == CX18_HW_Z8F0811_IR_HAUP)
  116. return cx18_i2c_new_ir(cx, adap, hw, type, hw_addrs[idx]);
  117. /* Is it not an I2C device or one we do not wish to register? */
  118. if (!hw_addrs[idx])
  119. return -1;
  120. /* It's an I2C device other than an analog tuner or IR chip */
  121. sd = v4l2_i2c_new_subdev(&cx->v4l2_dev, adap, type, hw_addrs[idx],
  122. NULL);
  123. if (sd != NULL)
  124. sd->grp_id = hw;
  125. return sd != NULL ? 0 : -1;
  126. }
  127. /* Find the first member of the subdev group id in hw */
  128. struct v4l2_subdev *cx18_find_hw(struct cx18 *cx, u32 hw)
  129. {
  130. struct v4l2_subdev *result = NULL;
  131. struct v4l2_subdev *sd;
  132. spin_lock(&cx->v4l2_dev.lock);
  133. v4l2_device_for_each_subdev(sd, &cx->v4l2_dev) {
  134. if (sd->grp_id == hw) {
  135. result = sd;
  136. break;
  137. }
  138. }
  139. spin_unlock(&cx->v4l2_dev.lock);
  140. return result;
  141. }
  142. static void cx18_setscl(void *data, int state)
  143. {
  144. struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
  145. int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
  146. u32 addr = bus_index ? CX18_REG_I2C_2_WR : CX18_REG_I2C_1_WR;
  147. u32 r = cx18_read_reg(cx, addr);
  148. if (state)
  149. cx18_write_reg(cx, r | SETSCL_BIT, addr);
  150. else
  151. cx18_write_reg(cx, r & ~SETSCL_BIT, addr);
  152. }
  153. static void cx18_setsda(void *data, int state)
  154. {
  155. struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
  156. int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
  157. u32 addr = bus_index ? CX18_REG_I2C_2_WR : CX18_REG_I2C_1_WR;
  158. u32 r = cx18_read_reg(cx, addr);
  159. if (state)
  160. cx18_write_reg(cx, r | SETSDL_BIT, addr);
  161. else
  162. cx18_write_reg(cx, r & ~SETSDL_BIT, addr);
  163. }
  164. static int cx18_getscl(void *data)
  165. {
  166. struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
  167. int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
  168. u32 addr = bus_index ? CX18_REG_I2C_2_RD : CX18_REG_I2C_1_RD;
  169. return cx18_read_reg(cx, addr) & GETSCL_BIT;
  170. }
  171. static int cx18_getsda(void *data)
  172. {
  173. struct cx18 *cx = ((struct cx18_i2c_algo_callback_data *)data)->cx;
  174. int bus_index = ((struct cx18_i2c_algo_callback_data *)data)->bus_index;
  175. u32 addr = bus_index ? CX18_REG_I2C_2_RD : CX18_REG_I2C_1_RD;
  176. return cx18_read_reg(cx, addr) & GETSDL_BIT;
  177. }
  178. /* template for i2c-bit-algo */
  179. static const struct i2c_adapter cx18_i2c_adap_template = {
  180. .name = "cx18 i2c driver",
  181. .algo = NULL, /* set by i2c-algo-bit */
  182. .algo_data = NULL, /* filled from template */
  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 const 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. /* init + register i2c adapter */
  196. int init_cx18_i2c(struct cx18 *cx)
  197. {
  198. int i, err;
  199. CX18_DEBUG_I2C("i2c init\n");
  200. for (i = 0; i < 2; i++) {
  201. /* Setup algorithm for adapter */
  202. cx->i2c_algo[i] = cx18_i2c_algo_template;
  203. cx->i2c_algo_cb_data[i].cx = cx;
  204. cx->i2c_algo_cb_data[i].bus_index = i;
  205. cx->i2c_algo[i].data = &cx->i2c_algo_cb_data[i];
  206. /* Setup adapter */
  207. cx->i2c_adap[i] = cx18_i2c_adap_template;
  208. cx->i2c_adap[i].algo_data = &cx->i2c_algo[i];
  209. sprintf(cx->i2c_adap[i].name + strlen(cx->i2c_adap[i].name),
  210. " #%d-%d", cx->instance, i);
  211. i2c_set_adapdata(&cx->i2c_adap[i], &cx->v4l2_dev);
  212. cx->i2c_adap[i].dev.parent = &cx->pci_dev->dev;
  213. }
  214. if (cx18_read_reg(cx, CX18_REG_I2C_2_WR) != 0x0003c02f) {
  215. /* Reset/Unreset I2C hardware block */
  216. /* Clock select 220MHz */
  217. cx18_write_reg_expect(cx, 0x10000000, 0xc71004,
  218. 0x00000000, 0x10001000);
  219. /* Clock Enable */
  220. cx18_write_reg_expect(cx, 0x10001000, 0xc71024,
  221. 0x00001000, 0x10001000);
  222. }
  223. /* courtesy of Steven Toth <stoth@hauppauge.com> */
  224. cx18_write_reg_expect(cx, 0x00c00000, 0xc7001c, 0x00000000, 0x00c000c0);
  225. mdelay(10);
  226. cx18_write_reg_expect(cx, 0x00c000c0, 0xc7001c, 0x000000c0, 0x00c000c0);
  227. mdelay(10);
  228. cx18_write_reg_expect(cx, 0x00c00000, 0xc7001c, 0x00000000, 0x00c000c0);
  229. mdelay(10);
  230. /* Set to edge-triggered intrs. */
  231. cx18_write_reg(cx, 0x00c00000, 0xc730c8);
  232. /* Clear any stale intrs */
  233. cx18_write_reg_expect(cx, HW2_I2C1_INT|HW2_I2C2_INT, HW2_INT_CLR_STATUS,
  234. ~(HW2_I2C1_INT|HW2_I2C2_INT), HW2_I2C1_INT|HW2_I2C2_INT);
  235. /* Hw I2C1 Clock Freq ~100kHz */
  236. cx18_write_reg(cx, 0x00021c0f & ~4, CX18_REG_I2C_1_WR);
  237. cx18_setscl(&cx->i2c_algo_cb_data[0], 1);
  238. cx18_setsda(&cx->i2c_algo_cb_data[0], 1);
  239. /* Hw I2C2 Clock Freq ~100kHz */
  240. cx18_write_reg(cx, 0x00021c0f & ~4, CX18_REG_I2C_2_WR);
  241. cx18_setscl(&cx->i2c_algo_cb_data[1], 1);
  242. cx18_setsda(&cx->i2c_algo_cb_data[1], 1);
  243. cx18_call_hw(cx, CX18_HW_GPIO_RESET_CTRL,
  244. core, reset, (u32) CX18_GPIO_RESET_I2C);
  245. err = i2c_bit_add_bus(&cx->i2c_adap[0]);
  246. if (err)
  247. goto err;
  248. err = i2c_bit_add_bus(&cx->i2c_adap[1]);
  249. if (err)
  250. goto err_del_bus_0;
  251. return 0;
  252. err_del_bus_0:
  253. i2c_del_adapter(&cx->i2c_adap[0]);
  254. err:
  255. return err;
  256. }
  257. void exit_cx18_i2c(struct cx18 *cx)
  258. {
  259. int i;
  260. CX18_DEBUG_I2C("i2c exit\n");
  261. cx18_write_reg(cx, cx18_read_reg(cx, CX18_REG_I2C_1_WR) | 4,
  262. CX18_REG_I2C_1_WR);
  263. cx18_write_reg(cx, cx18_read_reg(cx, CX18_REG_I2C_2_WR) | 4,
  264. CX18_REG_I2C_2_WR);
  265. for (i = 0; i < 2; i++) {
  266. i2c_del_adapter(&cx->i2c_adap[i]);
  267. }
  268. }
  269. /*
  270. Hauppauge HVR1600 should have:
  271. 32 cx24227
  272. 98 unknown
  273. a0 eeprom
  274. c2 tuner
  275. e? zilog ir
  276. */