cx18-i2c.c 9.3 KB

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