cx25840-firmware.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. static const char *get_fw_name(struct i2c_client *client)
  55. {
  56. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  57. if (firmware[0])
  58. return firmware;
  59. if (is_cx2388x(state))
  60. return "v4l-cx23885-avcore-01.fw";
  61. if (is_cx231xx(state))
  62. return "v4l-cx231xx-avcore-01.fw";
  63. return "v4l-cx25840.fw";
  64. }
  65. static int check_fw_load(struct i2c_client *client, int size)
  66. {
  67. /* DL_ADDR_HB DL_ADDR_LB */
  68. int s = cx25840_read(client, 0x801) << 8;
  69. s |= cx25840_read(client, 0x800);
  70. if (size != s) {
  71. v4l_err(client, "firmware %s load failed\n",
  72. get_fw_name(client));
  73. return -EINVAL;
  74. }
  75. v4l_info(client, "loaded %s firmware (%d bytes)\n",
  76. get_fw_name(client), size);
  77. return 0;
  78. }
  79. static int fw_write(struct i2c_client *client, const u8 *data, int size)
  80. {
  81. if (i2c_master_send(client, data, size) < size) {
  82. v4l_err(client, "firmware load i2c failure\n");
  83. return -ENOSYS;
  84. }
  85. return 0;
  86. }
  87. int cx25840_loadfw(struct i2c_client *client)
  88. {
  89. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  90. const struct firmware *fw = NULL;
  91. u8 buffer[FWSEND];
  92. const u8 *ptr;
  93. const char *fwname = get_fw_name(client);
  94. int size, retval;
  95. int MAX_BUF_SIZE = FWSEND;
  96. u32 gpio_oe = 0, gpio_da = 0;
  97. if (is_cx2388x(state)) {
  98. /* Preserve the GPIO OE and output bits */
  99. gpio_oe = cx25840_read(client, 0x160);
  100. gpio_da = cx25840_read(client, 0x164);
  101. }
  102. if (is_cx231xx(state) && MAX_BUF_SIZE > 16) {
  103. v4l_err(client, " Firmware download size changed to 16 bytes max length\n");
  104. MAX_BUF_SIZE = 16; /* cx231xx cannot accept more than 16 bytes at a time */
  105. }
  106. if (request_firmware(&fw, fwname, FWDEV(client)) != 0) {
  107. v4l_err(client, "unable to open firmware %s\n", fwname);
  108. return -EINVAL;
  109. }
  110. start_fw_load(client);
  111. buffer[0] = 0x08;
  112. buffer[1] = 0x02;
  113. size = fw->size;
  114. ptr = fw->data;
  115. while (size > 0) {
  116. int len = min(MAX_BUF_SIZE - 2, size);
  117. memcpy(buffer + 2, ptr, len);
  118. retval = fw_write(client, buffer, len + 2);
  119. if (retval < 0) {
  120. release_firmware(fw);
  121. return retval;
  122. }
  123. size -= len;
  124. ptr += len;
  125. }
  126. end_fw_load(client);
  127. size = fw->size;
  128. release_firmware(fw);
  129. if (is_cx2388x(state)) {
  130. /* Restore GPIO configuration after f/w load */
  131. cx25840_write(client, 0x160, gpio_oe);
  132. cx25840_write(client, 0x164, gpio_da);
  133. }
  134. return check_fw_load(client, size);
  135. }