isl6423.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Intersil ISL6423 SEC and LNB Power supply controller
  4. Copyright (C) Manu Abraham <abraham.manu@gmail.com>
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/errno.h>
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/string.h>
  12. #include <linux/slab.h>
  13. #include <media/dvb_frontend.h>
  14. #include "isl6423.h"
  15. static unsigned int verbose;
  16. module_param(verbose, int, 0644);
  17. MODULE_PARM_DESC(verbose, "Set Verbosity level");
  18. #define FE_ERROR 0
  19. #define FE_NOTICE 1
  20. #define FE_INFO 2
  21. #define FE_DEBUG 3
  22. #define FE_DEBUGREG 4
  23. #define dprintk(__y, __z, format, arg...) do { \
  24. if (__z) { \
  25. if ((verbose > FE_ERROR) && (verbose > __y)) \
  26. printk(KERN_ERR "%s: " format "\n", __func__ , ##arg); \
  27. else if ((verbose > FE_NOTICE) && (verbose > __y)) \
  28. printk(KERN_NOTICE "%s: " format "\n", __func__ , ##arg); \
  29. else if ((verbose > FE_INFO) && (verbose > __y)) \
  30. printk(KERN_INFO "%s: " format "\n", __func__ , ##arg); \
  31. else if ((verbose > FE_DEBUG) && (verbose > __y)) \
  32. printk(KERN_DEBUG "%s: " format "\n", __func__ , ##arg); \
  33. } else { \
  34. if (verbose > __y) \
  35. printk(format, ##arg); \
  36. } \
  37. } while (0)
  38. struct isl6423_dev {
  39. const struct isl6423_config *config;
  40. struct i2c_adapter *i2c;
  41. u8 reg_3;
  42. u8 reg_4;
  43. unsigned int verbose;
  44. };
  45. static int isl6423_write(struct isl6423_dev *isl6423, u8 reg)
  46. {
  47. struct i2c_adapter *i2c = isl6423->i2c;
  48. u8 addr = isl6423->config->addr;
  49. int err = 0;
  50. struct i2c_msg msg = { .addr = addr, .flags = 0, .buf = &reg, .len = 1 };
  51. dprintk(FE_DEBUG, 1, "write reg %02X", reg);
  52. err = i2c_transfer(i2c, &msg, 1);
  53. if (err < 0)
  54. goto exit;
  55. return 0;
  56. exit:
  57. dprintk(FE_ERROR, 1, "I/O error <%d>", err);
  58. return err;
  59. }
  60. static int isl6423_set_modulation(struct dvb_frontend *fe)
  61. {
  62. struct isl6423_dev *isl6423 = (struct isl6423_dev *) fe->sec_priv;
  63. const struct isl6423_config *config = isl6423->config;
  64. int err = 0;
  65. u8 reg_2 = 0;
  66. reg_2 = 0x01 << 5;
  67. if (config->mod_extern)
  68. reg_2 |= (1 << 3);
  69. else
  70. reg_2 |= (1 << 4);
  71. err = isl6423_write(isl6423, reg_2);
  72. if (err < 0)
  73. goto exit;
  74. return 0;
  75. exit:
  76. dprintk(FE_ERROR, 1, "I/O error <%d>", err);
  77. return err;
  78. }
  79. static int isl6423_voltage_boost(struct dvb_frontend *fe, long arg)
  80. {
  81. struct isl6423_dev *isl6423 = (struct isl6423_dev *) fe->sec_priv;
  82. u8 reg_3 = isl6423->reg_3;
  83. u8 reg_4 = isl6423->reg_4;
  84. int err = 0;
  85. if (arg) {
  86. /* EN = 1, VSPEN = 1, VBOT = 1 */
  87. reg_4 |= (1 << 4);
  88. reg_4 |= 0x1;
  89. reg_3 |= (1 << 3);
  90. } else {
  91. /* EN = 1, VSPEN = 1, VBOT = 0 */
  92. reg_4 |= (1 << 4);
  93. reg_4 &= ~0x1;
  94. reg_3 |= (1 << 3);
  95. }
  96. err = isl6423_write(isl6423, reg_3);
  97. if (err < 0)
  98. goto exit;
  99. err = isl6423_write(isl6423, reg_4);
  100. if (err < 0)
  101. goto exit;
  102. isl6423->reg_3 = reg_3;
  103. isl6423->reg_4 = reg_4;
  104. return 0;
  105. exit:
  106. dprintk(FE_ERROR, 1, "I/O error <%d>", err);
  107. return err;
  108. }
  109. static int isl6423_set_voltage(struct dvb_frontend *fe,
  110. enum fe_sec_voltage voltage)
  111. {
  112. struct isl6423_dev *isl6423 = (struct isl6423_dev *) fe->sec_priv;
  113. u8 reg_3 = isl6423->reg_3;
  114. u8 reg_4 = isl6423->reg_4;
  115. int err = 0;
  116. switch (voltage) {
  117. case SEC_VOLTAGE_OFF:
  118. /* EN = 0 */
  119. reg_4 &= ~(1 << 4);
  120. break;
  121. case SEC_VOLTAGE_13:
  122. /* EN = 1, VSPEN = 1, VTOP = 0, VBOT = 0 */
  123. reg_4 |= (1 << 4);
  124. reg_4 &= ~0x3;
  125. reg_3 |= (1 << 3);
  126. break;
  127. case SEC_VOLTAGE_18:
  128. /* EN = 1, VSPEN = 1, VTOP = 1, VBOT = 0 */
  129. reg_4 |= (1 << 4);
  130. reg_4 |= 0x2;
  131. reg_4 &= ~0x1;
  132. reg_3 |= (1 << 3);
  133. break;
  134. default:
  135. break;
  136. }
  137. err = isl6423_write(isl6423, reg_3);
  138. if (err < 0)
  139. goto exit;
  140. err = isl6423_write(isl6423, reg_4);
  141. if (err < 0)
  142. goto exit;
  143. isl6423->reg_3 = reg_3;
  144. isl6423->reg_4 = reg_4;
  145. return 0;
  146. exit:
  147. dprintk(FE_ERROR, 1, "I/O error <%d>", err);
  148. return err;
  149. }
  150. static int isl6423_set_current(struct dvb_frontend *fe)
  151. {
  152. struct isl6423_dev *isl6423 = (struct isl6423_dev *) fe->sec_priv;
  153. u8 reg_3 = isl6423->reg_3;
  154. const struct isl6423_config *config = isl6423->config;
  155. int err = 0;
  156. switch (config->current_max) {
  157. case SEC_CURRENT_275m:
  158. /* 275mA */
  159. /* ISELH = 0, ISELL = 0 */
  160. reg_3 &= ~0x3;
  161. break;
  162. case SEC_CURRENT_515m:
  163. /* 515mA */
  164. /* ISELH = 0, ISELL = 1 */
  165. reg_3 &= ~0x2;
  166. reg_3 |= 0x1;
  167. break;
  168. case SEC_CURRENT_635m:
  169. /* 635mA */
  170. /* ISELH = 1, ISELL = 0 */
  171. reg_3 &= ~0x1;
  172. reg_3 |= 0x2;
  173. break;
  174. case SEC_CURRENT_800m:
  175. /* 800mA */
  176. /* ISELH = 1, ISELL = 1 */
  177. reg_3 |= 0x3;
  178. break;
  179. }
  180. err = isl6423_write(isl6423, reg_3);
  181. if (err < 0)
  182. goto exit;
  183. switch (config->curlim) {
  184. case SEC_CURRENT_LIM_ON:
  185. /* DCL = 0 */
  186. reg_3 &= ~0x10;
  187. break;
  188. case SEC_CURRENT_LIM_OFF:
  189. /* DCL = 1 */
  190. reg_3 |= 0x10;
  191. break;
  192. }
  193. err = isl6423_write(isl6423, reg_3);
  194. if (err < 0)
  195. goto exit;
  196. isl6423->reg_3 = reg_3;
  197. return 0;
  198. exit:
  199. dprintk(FE_ERROR, 1, "I/O error <%d>", err);
  200. return err;
  201. }
  202. static void isl6423_release(struct dvb_frontend *fe)
  203. {
  204. isl6423_set_voltage(fe, SEC_VOLTAGE_OFF);
  205. kfree(fe->sec_priv);
  206. fe->sec_priv = NULL;
  207. }
  208. struct dvb_frontend *isl6423_attach(struct dvb_frontend *fe,
  209. struct i2c_adapter *i2c,
  210. const struct isl6423_config *config)
  211. {
  212. struct isl6423_dev *isl6423;
  213. isl6423 = kzalloc(sizeof(struct isl6423_dev), GFP_KERNEL);
  214. if (!isl6423)
  215. return NULL;
  216. isl6423->config = config;
  217. isl6423->i2c = i2c;
  218. fe->sec_priv = isl6423;
  219. /* SR3H = 0, SR3M = 1, SR3L = 0 */
  220. isl6423->reg_3 = 0x02 << 5;
  221. /* SR4H = 0, SR4M = 1, SR4L = 1 */
  222. isl6423->reg_4 = 0x03 << 5;
  223. if (isl6423_set_current(fe))
  224. goto exit;
  225. if (isl6423_set_modulation(fe))
  226. goto exit;
  227. fe->ops.release_sec = isl6423_release;
  228. fe->ops.set_voltage = isl6423_set_voltage;
  229. fe->ops.enable_high_lnb_voltage = isl6423_voltage_boost;
  230. isl6423->verbose = verbose;
  231. return fe;
  232. exit:
  233. kfree(isl6423);
  234. fe->sec_priv = NULL;
  235. return NULL;
  236. }
  237. EXPORT_SYMBOL(isl6423_attach);
  238. MODULE_DESCRIPTION("ISL6423 SEC");
  239. MODULE_AUTHOR("Manu Abraham");
  240. MODULE_LICENSE("GPL");