cx25840-firmware.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* cx25840 firmware functions
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License
  5. * as published by the Free Software Foundation; either version 2
  6. * of the License, or (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/module.h>
  14. #include <linux/i2c.h>
  15. #include <linux/firmware.h>
  16. #include <media/v4l2-common.h>
  17. #include <media/drv-intf/cx25840.h>
  18. #include "cx25840-core.h"
  19. /*
  20. * Mike Isely <isely@pobox.com> - The FWSEND parameter controls the
  21. * size of the firmware chunks sent down the I2C bus to the chip.
  22. * Previously this had been set to 1024 but unfortunately some I2C
  23. * implementations can't transfer data in such big gulps.
  24. * Specifically, the pvrusb2 driver has a hard limit of around 60
  25. * bytes, due to the encapsulation there of I2C traffic into USB
  26. * messages. So we have to significantly reduce this parameter.
  27. */
  28. #define FWSEND 48
  29. #define FWDEV(x) &((x)->dev)
  30. static char *firmware = "";
  31. module_param(firmware, charp, 0444);
  32. MODULE_PARM_DESC(firmware, "Firmware image to load");
  33. static void start_fw_load(struct i2c_client *client)
  34. {
  35. /* DL_ADDR_LB=0 DL_ADDR_HB=0 */
  36. cx25840_write(client, 0x800, 0x00);
  37. cx25840_write(client, 0x801, 0x00);
  38. // DL_MAP=3 DL_AUTO_INC=0 DL_ENABLE=1
  39. cx25840_write(client, 0x803, 0x0b);
  40. /* AUTO_INC_DIS=1 */
  41. cx25840_write(client, 0x000, 0x20);
  42. }
  43. static void end_fw_load(struct i2c_client *client)
  44. {
  45. /* AUTO_INC_DIS=0 */
  46. cx25840_write(client, 0x000, 0x00);
  47. /* DL_ENABLE=0 */
  48. cx25840_write(client, 0x803, 0x03);
  49. }
  50. #define CX2388x_FIRMWARE "v4l-cx23885-avcore-01.fw"
  51. #define CX231xx_FIRMWARE "v4l-cx231xx-avcore-01.fw"
  52. #define CX25840_FIRMWARE "v4l-cx25840.fw"
  53. static const char *get_fw_name(struct i2c_client *client)
  54. {
  55. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  56. if (firmware[0])
  57. return firmware;
  58. if (is_cx2388x(state))
  59. return CX2388x_FIRMWARE;
  60. if (is_cx231xx(state))
  61. return CX231xx_FIRMWARE;
  62. return CX25840_FIRMWARE;
  63. }
  64. static int check_fw_load(struct i2c_client *client, int size)
  65. {
  66. /* DL_ADDR_HB DL_ADDR_LB */
  67. int s = cx25840_read(client, 0x801) << 8;
  68. s |= cx25840_read(client, 0x800);
  69. if (size != s) {
  70. v4l_err(client, "firmware %s load failed\n",
  71. get_fw_name(client));
  72. return -EINVAL;
  73. }
  74. v4l_info(client, "loaded %s firmware (%d bytes)\n",
  75. get_fw_name(client), size);
  76. return 0;
  77. }
  78. static int fw_write(struct i2c_client *client, const u8 *data, int size)
  79. {
  80. if (i2c_master_send(client, data, size) < size) {
  81. v4l_err(client, "firmware load i2c failure\n");
  82. return -ENOSYS;
  83. }
  84. return 0;
  85. }
  86. int cx25840_loadfw(struct i2c_client *client)
  87. {
  88. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  89. const struct firmware *fw = NULL;
  90. u8 buffer[FWSEND];
  91. const u8 *ptr;
  92. const char *fwname = get_fw_name(client);
  93. int size, retval;
  94. int max_buf_size = FWSEND;
  95. u32 gpio_oe = 0, gpio_da = 0;
  96. if (is_cx2388x(state)) {
  97. /* Preserve the GPIO OE and output bits */
  98. gpio_oe = cx25840_read(client, 0x160);
  99. gpio_da = cx25840_read(client, 0x164);
  100. }
  101. /* cx231xx cannot accept more than 16 bytes at a time */
  102. if (is_cx231xx(state) && max_buf_size > 16)
  103. max_buf_size = 16;
  104. if (request_firmware(&fw, fwname, FWDEV(client)) != 0) {
  105. v4l_err(client, "unable to open firmware %s\n", fwname);
  106. return -EINVAL;
  107. }
  108. start_fw_load(client);
  109. buffer[0] = 0x08;
  110. buffer[1] = 0x02;
  111. size = fw->size;
  112. ptr = fw->data;
  113. while (size > 0) {
  114. int len = min(max_buf_size - 2, size);
  115. memcpy(buffer + 2, ptr, len);
  116. retval = fw_write(client, buffer, len + 2);
  117. if (retval < 0) {
  118. release_firmware(fw);
  119. return retval;
  120. }
  121. size -= len;
  122. ptr += len;
  123. }
  124. end_fw_load(client);
  125. size = fw->size;
  126. release_firmware(fw);
  127. if (is_cx2388x(state)) {
  128. /* Restore GPIO configuration after f/w load */
  129. cx25840_write(client, 0x160, gpio_oe);
  130. cx25840_write(client, 0x164, gpio_da);
  131. }
  132. return check_fw_load(client, size);
  133. }
  134. MODULE_FIRMWARE(CX2388x_FIRMWARE);
  135. MODULE_FIRMWARE(CX231xx_FIRMWARE);
  136. MODULE_FIRMWARE(CX25840_FIRMWARE);