cx18-i2c.c 12 KB

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