cx25840-firmware.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #define FWFILE "v4l-cx25840.fw"
  24. #define FWFILE_CX23885 "v4l-cx23885-avcore-01.fw"
  25. #define FWFILE_CX231XX "v4l-cx231xx-avcore-01.fw"
  26. /*
  27. * Mike Isely <isely@pobox.com> - The FWSEND parameter controls the
  28. * size of the firmware chunks sent down the I2C bus to the chip.
  29. * Previously this had been set to 1024 but unfortunately some I2C
  30. * implementations can't transfer data in such big gulps.
  31. * Specifically, the pvrusb2 driver has a hard limit of around 60
  32. * bytes, due to the encapsulation there of I2C traffic into USB
  33. * messages. So we have to significantly reduce this parameter.
  34. */
  35. #define FWSEND 48
  36. #define FWDEV(x) &((x)->dev)
  37. static char *firmware = FWFILE;
  38. module_param(firmware, charp, 0444);
  39. MODULE_PARM_DESC(firmware, "Firmware image [default: " FWFILE "]");
  40. static void start_fw_load(struct i2c_client *client)
  41. {
  42. /* DL_ADDR_LB=0 DL_ADDR_HB=0 */
  43. cx25840_write(client, 0x800, 0x00);
  44. cx25840_write(client, 0x801, 0x00);
  45. // DL_MAP=3 DL_AUTO_INC=0 DL_ENABLE=1
  46. cx25840_write(client, 0x803, 0x0b);
  47. /* AUTO_INC_DIS=1 */
  48. cx25840_write(client, 0x000, 0x20);
  49. }
  50. static void end_fw_load(struct i2c_client *client)
  51. {
  52. /* AUTO_INC_DIS=0 */
  53. cx25840_write(client, 0x000, 0x00);
  54. /* DL_ENABLE=0 */
  55. cx25840_write(client, 0x803, 0x03);
  56. }
  57. static int check_fw_load(struct i2c_client *client, int size)
  58. {
  59. /* DL_ADDR_HB DL_ADDR_LB */
  60. int s = cx25840_read(client, 0x801) << 8;
  61. s |= cx25840_read(client, 0x800);
  62. if (size != s) {
  63. v4l_err(client, "firmware %s load failed\n", firmware);
  64. return -EINVAL;
  65. }
  66. v4l_info(client, "loaded %s firmware (%d bytes)\n", firmware, size);
  67. return 0;
  68. }
  69. static int fw_write(struct i2c_client *client, const u8 *data, int size)
  70. {
  71. if (i2c_master_send(client, data, size) < size) {
  72. v4l_err(client, "firmware load i2c failure\n");
  73. return -ENOSYS;
  74. }
  75. return 0;
  76. }
  77. int cx25840_loadfw(struct i2c_client *client)
  78. {
  79. struct cx25840_state *state = to_state(i2c_get_clientdata(client));
  80. const struct firmware *fw = NULL;
  81. u8 buffer[FWSEND];
  82. const u8 *ptr;
  83. int size, retval;
  84. int MAX_BUF_SIZE = FWSEND;
  85. if (state->is_cx23885)
  86. firmware = FWFILE_CX23885;
  87. else if (state->is_cx231xx)
  88. firmware = FWFILE_CX231XX;
  89. if ((state->is_cx231xx) && MAX_BUF_SIZE > 16) {
  90. v4l_err(client, " Firmware download size changed to 16 bytes max length\n");
  91. MAX_BUF_SIZE = 16; /* cx231xx cannot accept more than 16 bytes at a time */
  92. }
  93. if (request_firmware(&fw, firmware, FWDEV(client)) != 0) {
  94. v4l_err(client, "unable to open firmware %s\n", firmware);
  95. return -EINVAL;
  96. }
  97. start_fw_load(client);
  98. buffer[0] = 0x08;
  99. buffer[1] = 0x02;
  100. size = fw->size;
  101. ptr = fw->data;
  102. while (size > 0) {
  103. int len = min(MAX_BUF_SIZE - 2, size);
  104. memcpy(buffer + 2, ptr, len);
  105. retval = fw_write(client, buffer, len + 2);
  106. if (retval < 0) {
  107. release_firmware(fw);
  108. return retval;
  109. }
  110. size -= len;
  111. ptr += len;
  112. }
  113. end_fw_load(client);
  114. size = fw->size;
  115. release_firmware(fw);
  116. return check_fw_load(client, size);
  117. }