cx25840-firmware.c 3.5 KB

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