cx25840-firmware.c 3.8 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/i2c-algo-bit.h>
  20. #include <linux/firmware.h>
  21. #include <media/v4l2-common.h>
  22. #include "cx25840.h"
  23. #define FWFILE "cx25840.fw"
  24. #define FWSEND 1024
  25. #define FWDEV(x) &((x)->adapter->dev)
  26. static int fastfw = 1;
  27. static char *firmware = FWFILE;
  28. module_param(fastfw, bool, 0444);
  29. module_param(firmware, charp, 0444);
  30. MODULE_PARM_DESC(fastfw, "Load firmware fast [0=100MHz 1=333MHz (default)]");
  31. MODULE_PARM_DESC(firmware, "Firmware image [default: " FWFILE "]");
  32. static inline void set_i2c_delay(struct i2c_client *client, int delay)
  33. {
  34. struct i2c_algo_bit_data *algod = client->adapter->algo_data;
  35. /* We aren't guaranteed to be using algo_bit,
  36. * so avoid the null pointer dereference
  37. * and disable the 'fast firmware load' */
  38. if (algod) {
  39. algod->udelay = delay;
  40. } else {
  41. fastfw = 0;
  42. }
  43. }
  44. static inline void start_fw_load(struct i2c_client *client)
  45. {
  46. /* DL_ADDR_LB=0 DL_ADDR_HB=0 */
  47. cx25840_write(client, 0x800, 0x00);
  48. cx25840_write(client, 0x801, 0x00);
  49. // DL_MAP=3 DL_AUTO_INC=0 DL_ENABLE=1
  50. cx25840_write(client, 0x803, 0x0b);
  51. /* AUTO_INC_DIS=1 */
  52. cx25840_write(client, 0x000, 0x20);
  53. if (fastfw)
  54. set_i2c_delay(client, 3);
  55. }
  56. static inline void end_fw_load(struct i2c_client *client)
  57. {
  58. if (fastfw)
  59. set_i2c_delay(client, 10);
  60. /* AUTO_INC_DIS=0 */
  61. cx25840_write(client, 0x000, 0x00);
  62. /* DL_ENABLE=0 */
  63. cx25840_write(client, 0x803, 0x03);
  64. }
  65. static inline 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. cx25840_err("firmware %s load failed\n", firmware);
  72. return -EINVAL;
  73. }
  74. cx25840_info("loaded %s firmware (%d bytes)\n", firmware, size);
  75. return 0;
  76. }
  77. static inline int fw_write(struct i2c_client *client, u8 * data, int size)
  78. {
  79. if (i2c_master_send(client, data, size) < size) {
  80. if (fastfw) {
  81. cx25840_err("333MHz i2c firmware load failed\n");
  82. fastfw = 0;
  83. set_i2c_delay(client, 10);
  84. if (i2c_master_send(client, data, size) < size) {
  85. cx25840_err
  86. ("100MHz i2c firmware load failed\n");
  87. return -ENOSYS;
  88. }
  89. } else {
  90. cx25840_err("firmware load i2c failure\n");
  91. return -ENOSYS;
  92. }
  93. }
  94. return 0;
  95. }
  96. int cx25840_loadfw(struct i2c_client *client)
  97. {
  98. const struct firmware *fw = NULL;
  99. u8 buffer[4], *ptr;
  100. int size, send, retval;
  101. if (request_firmware(&fw, firmware, FWDEV(client)) != 0) {
  102. cx25840_err("unable to open firmware %s\n", firmware);
  103. return -EINVAL;
  104. }
  105. start_fw_load(client);
  106. buffer[0] = 0x08;
  107. buffer[1] = 0x02;
  108. buffer[2] = fw->data[0];
  109. buffer[3] = fw->data[1];
  110. retval = fw_write(client, buffer, 4);
  111. if (retval < 0) {
  112. release_firmware(fw);
  113. return retval;
  114. }
  115. size = fw->size - 2;
  116. ptr = fw->data;
  117. while (size > 0) {
  118. ptr[0] = 0x08;
  119. ptr[1] = 0x02;
  120. send = size > (FWSEND - 2) ? FWSEND : size + 2;
  121. retval = fw_write(client, ptr, send);
  122. if (retval < 0) {
  123. release_firmware(fw);
  124. return retval;
  125. }
  126. size -= FWSEND - 2;
  127. ptr += FWSEND - 2;
  128. }
  129. end_fw_load(client);
  130. size = fw->size;
  131. release_firmware(fw);
  132. return check_fw_load(client, size);
  133. }