cx18-i2c.c 9.8 KB

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