cx25840-firmware.c 4.4 KB

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