ice40xx.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  1. /*
  2. * driver/irda_ice40 IR Led driver
  3. *
  4. * Copyright (C) 2012 Samsung Electronics
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/err.h>
  24. #include <linux/i2c.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/irq.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/slab.h>
  29. #include <linux/delay.h>
  30. #include <linux/workqueue.h>
  31. #include <linux/device.h>
  32. #include <linux/earlysuspend.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/gpio.h>
  35. #include <linux/uaccess.h>
  36. #include <linux/fs.h>
  37. #include <linux/clk.h>
  38. #include <linux/firmware.h>
  39. #include <linux/regulator/consumer.h>
  40. #include <linux/of_gpio.h>
  41. #include <linux/err.h>
  42. #include <linux/miscdevice.h>
  43. /* #define IRDA_RX_ENABLE 1 */
  44. #ifdef IRDA_RX_ENABLE
  45. #include <linux/switch.h>
  46. #endif
  47. #include <linux/irda_ice40.h>
  48. #if defined(TEST_DEBUG)
  49. #define pr_irda pr_emerg
  50. #else
  51. #define pr_irda pr_info
  52. #endif
  53. #ifdef IRDA_RX_ENABLE
  54. struct switch_dev switch_irda_receive = {
  55. .name = "irda_receive",
  56. };
  57. #endif
  58. struct irda_ice40_data {
  59. struct miscdevice miscdev;
  60. struct i2c_client *client;
  61. struct workqueue_struct *firmware_dl;
  62. struct delayed_work fw_dl;
  63. const struct firmware *fw;
  64. struct mutex mutex;
  65. struct {
  66. unsigned char addr;
  67. unsigned char data[MAX_SIZE];
  68. } i2c_block_transfer;
  69. int length;
  70. int count;
  71. int operation;
  72. #ifdef IRDA_RX_ENABLE
  73. int learn_cnt;
  74. #endif
  75. int dev_id;
  76. int ir_freq;
  77. int ir_sum;
  78. int on_off;
  79. };
  80. static int g_ack_number;
  81. static int count_number;
  82. static struct irda_ice40_platform_data *g_pdata;
  83. static struct irda_ice40_data *g_data;
  84. static int Is_clk_enabled;
  85. static int enable_counte;
  86. static struct mutex en_mutex;
  87. #ifdef IRDA_RX_ENABLE
  88. static unsigned char learning_buf[1024];
  89. #endif
  90. static int ice40_clock_en(int onoff)
  91. {
  92. static struct clk *fpga_main_src_clk;
  93. static struct clk *fpga_main_clk;
  94. pr_info("%s:%d - on : %d\n", __func__, __LINE__, onoff);
  95. #if defined(CONFIG_MACH_K3GDUOS_CTC)
  96. fpga_main_clk = NULL;
  97. if (!fpga_main_src_clk)
  98. fpga_main_src_clk = clk_get(NULL, "fpga_src_clk");
  99. if (IS_ERR(fpga_main_src_clk))
  100. pr_err("%s: unable to get fpga_main_src_clk\n", __func__);
  101. if (onoff) {
  102. clk_set_rate(fpga_main_src_clk, 24000000);
  103. clk_prepare_enable(fpga_main_src_clk);
  104. } else {
  105. clk_disable_unprepare(fpga_main_src_clk);
  106. clk_put(fpga_main_src_clk);
  107. fpga_main_src_clk = NULL;
  108. }
  109. #else
  110. if (!fpga_main_src_clk)
  111. fpga_main_src_clk = clk_get(NULL, "gp2_src_clk");
  112. if (IS_ERR(fpga_main_src_clk))
  113. pr_err("%s: unable to get fpga_main_src_clk\n", __func__);
  114. if (!fpga_main_clk)
  115. fpga_main_clk = clk_get(NULL, "gp2_clk");
  116. if (IS_ERR(fpga_main_clk))
  117. pr_err("%s: unable to get fpga_main_clk\n", __func__);
  118. if (onoff) {
  119. clk_set_rate(fpga_main_src_clk, 24000000);
  120. clk_prepare_enable(fpga_main_clk);
  121. } else {
  122. clk_disable_unprepare(fpga_main_clk);
  123. clk_put(fpga_main_src_clk);
  124. clk_put(fpga_main_clk);
  125. fpga_main_src_clk = NULL;
  126. fpga_main_clk = NULL;
  127. }
  128. #endif
  129. return 0;
  130. }
  131. static void fpga_enable(int enable_clk, int enable_rst_n)
  132. {
  133. int ret;
  134. if (enable_clk) {
  135. if (!Is_clk_enabled && (enable_counte == 0)) {
  136. mutex_lock(&en_mutex);
  137. ret = ice40_clock_en(1);
  138. if (enable_rst_n)
  139. gpio_set_value(g_pdata->rst_n, GPIO_LEVEL_LOW);
  140. usleep_range(1000, 2000);
  141. Is_clk_enabled = 1;
  142. }
  143. enable_counte++;
  144. } else {
  145. if (Is_clk_enabled && (enable_counte == 1)) {
  146. Is_clk_enabled = 0;
  147. usleep_range(2000, 2500);
  148. gpio_set_value(g_pdata->rst_n, GPIO_LEVEL_HIGH);
  149. ret = ice40_clock_en(0);
  150. mutex_unlock(&en_mutex);
  151. }
  152. if (enable_counte < 0) {
  153. printk(KERN_ERR "%s enable_counte ERR!= %d\n",
  154. __func__, enable_counte);
  155. enable_counte = 0;
  156. } else {
  157. enable_counte--;
  158. }
  159. }
  160. }
  161. static void irled_power_onoff(int onoff)
  162. {
  163. int ret;
  164. static struct regulator *reg_l19;
  165. if (!reg_l19) {
  166. reg_l19 = regulator_get(NULL, "8084_l19");
  167. ret = regulator_set_voltage(reg_l19, 3300000, 3300000);
  168. if (IS_ERR(reg_l19)) {
  169. printk(KERN_ERR"could not get 8084_l19, rc = %ld\n",
  170. PTR_ERR(reg_l19));
  171. return;
  172. }
  173. }
  174. if (onoff) {
  175. ret = regulator_enable(reg_l19);
  176. if (ret) {
  177. printk(KERN_ERR"enable l19 failed, rc=%d\n", ret);
  178. return;
  179. }
  180. printk(KERN_DEBUG"ir_led power_on is finished.\n");
  181. } else {
  182. if (regulator_is_enabled(reg_l19)) {
  183. ret = regulator_disable(reg_l19);
  184. if (ret) {
  185. printk(KERN_ERR"disable l19 failed, rc=%d\n",
  186. ret);
  187. return;
  188. }
  189. }
  190. printk(KERN_DEBUG"ir_led power_off is finished.\n");
  191. }
  192. }
  193. #ifdef CONFIG_OF
  194. static int irda_ice40_parse_dt(struct device *dev,
  195. struct irda_ice40_platform_data *pdata)
  196. {
  197. struct device_node *np = dev->of_node;
  198. int ret;
  199. ret = of_property_read_u32(np, "irda_ice40,fw_ver", &pdata->fw_ver);
  200. if (ret < 0) {
  201. pr_err("[%s]: failed to read fw_ver\n", __func__);
  202. return ret;
  203. }
  204. pdata->rst_n = of_get_named_gpio(np, "irda_ice40,reset_n", 0);
  205. pdata->spi_clk = of_get_named_gpio(np, "irda_ice40,scl-gpio", 0);
  206. pdata->spi_si = of_get_named_gpio(np, "irda_ice40,sda-gpio", 0);
  207. pdata->irda_irq = of_get_named_gpio(np, "irda_ice40,irq-gpio", 0);
  208. pdata->cresetb = of_get_named_gpio(np, "irda_ice40,cresetb", 0);
  209. #ifdef CONFIG_MACH_KLTE_VZW
  210. ret = of_property_read_u32(np,
  211. "tunable,support", &pdata->tunable_support);
  212. if (ret < 0) {
  213. pr_err("[%s]: failed to read tunable\n", __func__);
  214. return ret;
  215. }
  216. pdata->tunable_crstb = of_get_named_gpio(np, "tunable,cresetb", 0);
  217. #endif
  218. return 0;
  219. }
  220. #else
  221. static int irda_ice40_parse_dt(struct device *dev,
  222. struct irda_ice40_platform_data *pdata)
  223. {
  224. return -ENODEV;
  225. }
  226. #endif
  227. static void irda_ice40_config(void)
  228. {
  229. int rc = 0;
  230. pr_info("%s\n", __func__);
  231. pr_info("g_pdata->fw_ver = %d\n", g_pdata->fw_ver);
  232. pr_info("g_pdata->rst_n = %d\n", g_pdata->rst_n);
  233. pr_info("g_pdata->spi_clk = %d\n", g_pdata->spi_clk);
  234. pr_info("g_pdata->spi_si = %d\n", g_pdata->spi_si);
  235. pr_info("g_pdata->irda_irq= %d\n", g_pdata->irda_irq);
  236. pr_info("g_pdata->cresetb = %d\n", g_pdata->cresetb);
  237. #ifdef CONFIG_MACH_KLTE_VZW
  238. pr_info("g_pdata->tunable_support = %d\n", g_pdata->tunable_support);
  239. pr_info("g_pdata->tunable_crstb= %d\n", g_pdata->tunable_crstb);
  240. #endif
  241. rc = gpio_tlmm_config(GPIO_CFG(g_pdata->spi_si, 0,
  242. GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL,
  243. GPIO_CFG_2MA), 1);
  244. if (rc)
  245. pr_err("%s: error : %d\n", __func__, rc);
  246. rc = gpio_tlmm_config(GPIO_CFG(g_pdata->spi_clk, 0,
  247. GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL,
  248. GPIO_CFG_2MA), 1);
  249. if (rc)
  250. pr_err("%s: error : %d\n", __func__, rc);
  251. #if defined(CONFIG_MACH_K3GDUOS_CTC)
  252. rc = gpio_tlmm_config(GPIO_CFG(GPIO_FPGA_MAIN_CLK_CTC_REV02, 1,
  253. GPIO_CFG_OUTPUT, GPIO_CFG_PULL_DOWN,
  254. GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  255. #else
  256. rc = gpio_tlmm_config(GPIO_CFG(GPIO_FPGA_MAIN_CLK, 2,
  257. GPIO_CFG_OUTPUT, GPIO_CFG_PULL_DOWN,
  258. GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  259. #endif
  260. if (rc)
  261. pr_err("%s: error : %d\n", __func__, rc);
  262. rc = gpio_tlmm_config(GPIO_CFG(g_pdata->cresetb, 0,
  263. GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL,
  264. GPIO_CFG_2MA), 1);
  265. if (rc)
  266. pr_warning("%s: warning check pin num[%d]\n",
  267. __func__, rc);
  268. rc = gpio_request(g_pdata->cresetb, "irda_creset");
  269. if (rc)
  270. pr_err("%s: error : %d\n", __func__, rc);
  271. rc = gpio_direction_output(g_pdata->cresetb, 1);
  272. if (rc)
  273. pr_err("%s: error : %d\n", __func__, rc);
  274. rc = gpio_tlmm_config(GPIO_CFG(g_pdata->rst_n, 0,
  275. GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL,
  276. GPIO_CFG_2MA), 1);
  277. if (rc)
  278. pr_warning("%s: warning check pin num[%d]\n",
  279. __func__, rc);
  280. rc = gpio_request(g_pdata->rst_n, "irda_rst_n");
  281. if (rc)
  282. pr_err("%s: error : %d\n", __func__, rc);
  283. rc = gpio_direction_output(g_pdata->rst_n, 0);
  284. if (rc)
  285. pr_err("%s: error : %d\n", __func__, rc);
  286. rc = gpio_tlmm_config(GPIO_CFG(g_pdata->irda_irq, 0,
  287. GPIO_CFG_INPUT, GPIO_CFG_NO_PULL,
  288. GPIO_CFG_2MA), 1);
  289. if (rc)
  290. pr_err("%s: error : %d\n", __func__, rc);
  291. rc = gpio_request(g_pdata->irda_irq, "irda_irq");
  292. if (rc)
  293. pr_err("%s: error : %d\n", __func__, rc);
  294. rc = gpio_direction_input(g_pdata->irda_irq);
  295. if (rc)
  296. pr_err("%s: error : %d\n", __func__, rc);
  297. #ifdef CONFIG_MACH_KLTE_VZW
  298. if (g_pdata->tunable_support) {
  299. rc = gpio_tlmm_config(GPIO_CFG(g_pdata->tunable_crstb, 0,
  300. GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL,
  301. GPIO_CFG_2MA), 1);
  302. if (rc)
  303. pr_warning("%s: warning check pin num[%d]\n",
  304. __func__, rc);
  305. rc = gpio_request(g_pdata->tunable_crstb, "tunable_creset");
  306. if (rc)
  307. pr_err("%s: error : %d\n", __func__, rc);
  308. rc = gpio_direction_output(g_pdata->tunable_crstb, 0);
  309. if (rc)
  310. pr_err("%s: error : %d\n", __func__, rc);
  311. }
  312. #endif
  313. }
  314. /*
  315. * Send ice40 fpga firmware data thougth spi communication
  316. */
  317. static int ice40_fpga_send_firmware_data(const u8 *data, int len)
  318. {
  319. unsigned int i, j;
  320. unsigned char spibit;
  321. i = 0;
  322. while (i < len) {
  323. j = 0;
  324. spibit = data[i];
  325. while (j < 8) {
  326. gpio_set_value_cansleep(g_pdata->spi_clk,
  327. GPIO_LEVEL_LOW);
  328. if (spibit & 0x80)
  329. gpio_set_value_cansleep(g_pdata->spi_si,
  330. GPIO_LEVEL_HIGH);
  331. else
  332. gpio_set_value_cansleep(g_pdata->spi_si,
  333. GPIO_LEVEL_LOW);
  334. j = j+1;
  335. gpio_set_value_cansleep(g_pdata->spi_clk,
  336. GPIO_LEVEL_HIGH);
  337. spibit = spibit<<1;
  338. }
  339. i = i+1;
  340. }
  341. gpio_set_value_cansleep(g_pdata->spi_si, GPIO_LEVEL_HIGH);
  342. i = 0;
  343. while (i < 200) {
  344. gpio_set_value_cansleep(g_pdata->spi_clk, GPIO_LEVEL_LOW);
  345. i = i+1;
  346. gpio_set_value_cansleep(g_pdata->spi_clk, GPIO_LEVEL_HIGH);
  347. }
  348. return 0;
  349. }
  350. static int ice40_fpga_fimrware_update_start(const u8 *data, int len)
  351. {
  352. int retry = FIRMWARE_MAX_RETRY;
  353. pr_irda("%s\n", __func__);
  354. fpga_enable(1, 0);
  355. do {
  356. gpio_set_value(g_pdata->rst_n, GPIO_LEVEL_LOW);
  357. usleep_range(30, 50);
  358. gpio_set_value(g_pdata->cresetb, GPIO_LEVEL_LOW);
  359. usleep_range(30, 50);
  360. gpio_set_value(g_pdata->cresetb, GPIO_LEVEL_HIGH);
  361. usleep_range(1000, 1300);
  362. ice40_fpga_send_firmware_data(data, len);
  363. usleep_range(50, 70);
  364. udelay(5);
  365. pr_irda("FPGA firmware update success\n");
  366. break;
  367. } while (retry);
  368. fpga_enable(0, 0);
  369. return 0;
  370. }
  371. void ice40_fpga_firmware_update_klte(void)
  372. {
  373. struct i2c_client *client = g_data->client;
  374. switch (g_pdata->fw_ver) {
  375. case 1:
  376. pr_irda("%s[%d] fw_ver %d\n", __func__,
  377. __LINE__, g_pdata->fw_ver);
  378. if (request_firmware(&g_data->fw,
  379. "ice40xx/i2c_top_bitmap_1.fw", &client->dev))
  380. pr_err("%s: Can't open firmware file\n", __func__);
  381. else
  382. ice40_fpga_fimrware_update_start(g_data->fw->data,
  383. g_data->fw->size);
  384. release_firmware(g_data->fw);
  385. break;
  386. case 2:
  387. pr_irda("%s[%d] fw_ver %d\n", __func__,
  388. __LINE__, g_pdata->fw_ver);
  389. if (request_firmware(&g_data->fw,
  390. "ice40xx/i2c_top_bitmap_2.fw", &client->dev))
  391. pr_err("%s: Can't open firmware file\n", __func__);
  392. else
  393. ice40_fpga_fimrware_update_start(g_data->fw->data,
  394. g_data->fw->size);
  395. release_firmware(g_data->fw);
  396. break;
  397. default:
  398. pr_err("[%s] Not supported [fw_ver = %d]\n",
  399. __func__, g_pdata->fw_ver);
  400. break;
  401. }
  402. usleep_range(10000, 12000);
  403. }
  404. static ssize_t ice40_fpga_fw_update_store(struct device *dev,
  405. struct device_attribute *attr,
  406. const char *buf, size_t size)
  407. {
  408. struct file *fp = NULL;
  409. long fsize = 0, nread = 0;
  410. const u8 *buff = 0;
  411. char fw_path[SEC_FPGA_MAX_FW_PATH];
  412. int locate, ret, rc;
  413. mm_segment_t old_fs = get_fs();
  414. pr_irda("%s\n", __func__);
  415. ret = sscanf(buf, "%d", &locate);
  416. if (!ret) {
  417. pr_err("[%s] force select extSdCard\n", __func__);
  418. locate = 0;
  419. }
  420. old_fs = get_fs();
  421. set_fs(get_ds());
  422. if (locate) {
  423. snprintf(fw_path, SEC_FPGA_MAX_FW_PATH,
  424. "/storage/sdcard0/%s", SEC_FPGA_FW_FILENAME);
  425. } else {
  426. snprintf(fw_path, SEC_FPGA_MAX_FW_PATH,
  427. "/storage/extSdCard/%s", SEC_FPGA_FW_FILENAME);
  428. }
  429. fp = filp_open(fw_path, O_RDONLY, 0);
  430. if (IS_ERR(fp)) {
  431. pr_err("file %s open error:%d\n",
  432. fw_path, (s32)fp);
  433. goto err_open;
  434. }
  435. fsize = fp->f_path.dentry->d_inode->i_size;
  436. pr_irda("fpga firmware size: %ld\n", fsize);
  437. buff = kzalloc((size_t)fsize, GFP_KERNEL);
  438. if (!buff) {
  439. pr_err("fail to alloc buffer for fw\n");
  440. goto err_alloc;
  441. }
  442. nread = vfs_read(fp, (char __user *)buff, fsize, &fp->f_pos);
  443. if (nread != fsize) {
  444. pr_err("fail to read file %s (nread = %ld)\n",
  445. fw_path, nread);
  446. goto err_fw_size;
  447. }
  448. rc = gpio_tlmm_config(GPIO_CFG(g_pdata->spi_si, 0,
  449. GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL,
  450. GPIO_CFG_2MA), 1);
  451. if (rc)
  452. pr_err("%s: error : %d\n", __func__, rc);
  453. rc = gpio_tlmm_config(GPIO_CFG(g_pdata->spi_clk, 0,
  454. GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL,
  455. GPIO_CFG_2MA), 1);
  456. if (rc)
  457. pr_err("%s: error : %d\n", __func__, rc);
  458. ice40_fpga_fimrware_update_start((unsigned char *)buff, fsize);
  459. err_fw_size:
  460. kfree(buff);
  461. err_alloc:
  462. filp_close(fp, NULL);
  463. err_open:
  464. set_fs(old_fs);
  465. return size;
  466. }
  467. static ssize_t ice40_fpga_fw_update_show(struct device *dev,
  468. struct device_attribute *attr,
  469. char *buf)
  470. {
  471. return strlen(buf);
  472. }
  473. static int irda_ice40_read(struct i2c_client *client, u16 slave_addr,
  474. u16 reg_addr, u16 length, u8 *value)
  475. {
  476. struct i2c_msg msg[2];
  477. int ret;
  478. pr_irda("client address before read %u\n", client->addr);
  479. *value = 0;
  480. client->addr = slave_addr;
  481. msg[0].addr = client->addr;
  482. msg[0].flags = 0x00;
  483. msg[0].len = 1;
  484. msg[0].buf = (u8 *)&reg_addr;
  485. msg[1].addr = client->addr;
  486. msg[1].flags = I2C_M_RD | I2C_CLIENT_PEC;
  487. msg[1].len = length;
  488. msg[1].buf = (u8 *)value;
  489. fpga_enable(1, 1);
  490. ret = i2c_transfer(client->adapter, msg, 2);
  491. if (ret != 2) {
  492. pr_irda("%s: err1 %d\n", __func__, ret);
  493. ret = i2c_transfer(client->adapter, msg, 2);
  494. if (ret != 2) {
  495. pr_irda("%s: err2 %d\n", __func__, ret);
  496. fpga_enable(0, 0);
  497. return -ret;
  498. } else {
  499. fpga_enable(0, 0);
  500. return 0;
  501. }
  502. } else {
  503. fpga_enable(0, 0);
  504. return 0;
  505. }
  506. }
  507. static ssize_t ice40_ver_check_show(struct device *dev,
  508. struct device_attribute *attr,
  509. char *buf)
  510. {
  511. struct irda_ice40_data *data = dev_get_drvdata(dev);
  512. char *bufp = buf;
  513. u8 fw_ver, read_val;
  514. irda_ice40_read(data->client, IRDA_I2C_ADDR, FW_VER_ADDR, 1, &read_val);
  515. pr_irda("%s Actual value read 0x%x\n", __func__, read_val);
  516. bufp += snprintf(bufp, SNPRINT_BUF_SIZE, "val 0x%x,", read_val);
  517. bufp += snprintf(bufp, SNPRINT_BUF_SIZE,
  518. "operation 0x%x,", read_val&0x3);
  519. fw_ver = (read_val >> 2) & 0x3;
  520. bufp += snprintf(bufp, SNPRINT_BUF_SIZE, "ver %d\n", fw_ver + 11);
  521. irda_ice40_read(data->client, IRDA_I2C_ADDR, 0x00, 1, &read_val);
  522. fw_ver = (read_val >> 4) & 0xf;
  523. bufp += snprintf(bufp, SNPRINT_BUF_SIZE, "0x00 read ver %d\n", fw_ver);
  524. return strlen(buf);
  525. }
  526. static void fw_work(struct work_struct *work)
  527. {
  528. ice40_fpga_firmware_update_klte();
  529. Is_clk_enabled = 0;
  530. }
  531. static int ir_remocon_work(struct irda_ice40_data *ir_data, int count)
  532. {
  533. struct irda_ice40_data *data = ir_data;
  534. struct i2c_client *client = data->client;
  535. int buf_size = count;
  536. int ret;
  537. int emission_time;
  538. int ack_pin_onoff;
  539. int ack_number;
  540. int f_checksum;
  541. int retry;
  542. if (count_number >= 100)
  543. count_number = 0;
  544. count_number++;
  545. pr_irda("%s: total buf_size: %d\n", __func__, buf_size);
  546. fpga_enable(1, 1);
  547. irled_power_onoff(POWER_ON);
  548. mutex_lock(&data->mutex);
  549. client->addr = IRDA_I2C_ADDR;
  550. data->i2c_block_transfer.addr = 0x00;
  551. data->i2c_block_transfer.data[0] = (count >> 8) & 0xFF;
  552. data->i2c_block_transfer.data[1] = count & 0xFF;
  553. buf_size++;
  554. f_checksum = 0;
  555. retry = 0;
  556. while (!f_checksum) {
  557. ret = i2c_master_send(client,
  558. (unsigned char *) &(data->i2c_block_transfer), buf_size);
  559. if (ret < 0) {
  560. dev_err(&client->dev, "%s: err1 %d\n", __func__, ret);
  561. ret = i2c_master_send(client,
  562. (unsigned char *) &(data->i2c_block_transfer), buf_size);
  563. if (ret < 0) {
  564. dev_err(&client->dev, "%s: err1 %d\n", __func__, ret);
  565. ret = i2c_master_send(client,
  566. data->i2c_block_transfer.data, count);
  567. if (ret < 0)
  568. dev_err(&client->dev, "%s: err2 %d\n",
  569. __func__, ret);
  570. }
  571. }
  572. usleep_range(10000, 12000);
  573. ack_pin_onoff = 0;
  574. if (gpio_get_value(g_pdata->irda_irq)) {
  575. ack_pin_onoff = 1;
  576. retry++;
  577. } else {
  578. ack_pin_onoff = 2;
  579. f_checksum = 1;
  580. }
  581. if (retry > 5)
  582. break;
  583. }
  584. if (ack_pin_onoff == 1)
  585. pr_irda("%s : %d %d Checksum NG!\n",
  586. __func__, count_number, retry);
  587. else {
  588. if (!retry)
  589. pr_irda("%s : %d %d Checksum OK!\n",
  590. __func__, count_number, retry);
  591. else
  592. pr_irda("%s : %d %d Checksum RE!\n",
  593. __func__, count_number, retry);
  594. }
  595. ack_number = ack_pin_onoff;
  596. mutex_unlock(&data->mutex);
  597. emission_time = (1000 * (data->ir_sum) / (data->ir_freq));
  598. if (emission_time > 0)
  599. msleep(emission_time);
  600. pr_irda("%s: emission_time = %d\n",
  601. __func__, emission_time);
  602. retry = 0;
  603. while (!gpio_get_value(g_pdata->irda_irq)) {
  604. usleep_range(100000, 120000);
  605. pr_irda("%s : try to check irda_irq %d, %d\n",
  606. __func__, emission_time, retry);
  607. if (retry++ > 5)
  608. break;
  609. }
  610. if (gpio_get_value(g_pdata->irda_irq)) {
  611. pr_irda("%s : %d Sending IR OK!\n",
  612. __func__, count_number);
  613. ack_pin_onoff = 4;
  614. } else {
  615. pr_irda("%s : %d Sending IR NG!\n",
  616. __func__, count_number);
  617. ack_pin_onoff = 2;
  618. }
  619. ack_number += ack_pin_onoff;
  620. data->ir_freq = 0;
  621. data->ir_sum = 0;
  622. data->count = 0;
  623. data->length = 0;
  624. data->operation = 0xffff;
  625. irled_power_onoff(POWER_OFF);
  626. fpga_enable(0, 0);
  627. g_ack_number = ack_number;
  628. if (ack_number == 6)
  629. return SEND_SUCCESS;
  630. else
  631. return SEND_FAIL;
  632. }
  633. static ssize_t remocon_store(struct device *dev, struct device_attribute *attr,
  634. const char *buf, size_t size)
  635. {
  636. struct irda_ice40_data *data = dev_get_drvdata(dev);
  637. unsigned int _data;
  638. unsigned int count = 2, i = 0;
  639. unsigned int c_factor = 0;
  640. unsigned int temp_data = 0;
  641. int ret;
  642. pr_irda("%s ir_send called[%d]\n", __func__, __LINE__);
  643. for (i = 0; i < MAX_SIZE; i++) {
  644. if (sscanf(buf++, "%u", &_data) == 1) {
  645. if (_data == 0 || buf == '\0')
  646. break;
  647. if (count == 2) {
  648. data->ir_freq = _data;
  649. data->operation = IRDA_SINGLE;
  650. /* operation cmd */
  651. /* single mode */
  652. data->i2c_block_transfer.data[2]
  653. = IRDA_SINGLE;
  654. /* frequency cmd 15~8 */
  655. data->i2c_block_transfer.data[3]
  656. = (_data >> 8) & 0xFF;
  657. /* frequency cmd 7~0 */
  658. data->i2c_block_transfer.data[4]
  659. = _data & 0xFF;
  660. count += 3;
  661. } else {
  662. c_factor = 1000000 / data->ir_freq;
  663. temp_data = _data / c_factor;
  664. data->ir_sum += temp_data;
  665. data->i2c_block_transfer.data[count++] = (temp_data >> 8);
  666. data->i2c_block_transfer.data[count++] = temp_data & 0xFF;
  667. }
  668. while (_data > 0) {
  669. buf++;
  670. _data /= 10;
  671. }
  672. } else {
  673. break;
  674. }
  675. }
  676. data->count = count;
  677. ret = ir_remocon_work(data, data->count);
  678. if (ret < 0)
  679. pr_info("%s, failed Send ir led\n", __func__);
  680. return size;
  681. }
  682. static ssize_t remocon_show(struct device *dev, struct device_attribute *attr,
  683. char *buf)
  684. {
  685. struct irda_ice40_data *data = dev_get_drvdata(dev);
  686. int i;
  687. char *bufp = buf;
  688. for (i = 5; i < MAX_SIZE - 1; i++) {
  689. if (data->i2c_block_transfer.data[i] == 0
  690. && data->i2c_block_transfer.data[i+1] == 0)
  691. break;
  692. else
  693. bufp += snprintf(bufp, SNPRINT_BUF_SIZE, "%u,",
  694. data->i2c_block_transfer.data[i]);
  695. }
  696. return strlen(buf);
  697. }
  698. /* sysfs node ir_send_result */
  699. static ssize_t remocon_ack(struct device *dev, struct device_attribute *attr,
  700. char *buf)
  701. {
  702. pr_irda("%s : g_ack_number = %d\n", __func__, g_ack_number);
  703. if (g_ack_number == 6)
  704. return snprintf(buf, SNPRINT_BUF_SIZE, "1\n");
  705. else
  706. return snprintf(buf, SNPRINT_BUF_SIZE, "0\n");
  707. }
  708. static int irda_read_device_info(struct irda_ice40_data *ir_data)
  709. {
  710. struct irda_ice40_data *data = ir_data;
  711. struct i2c_client *client = data->client;
  712. u8 buf_ir_test[8];
  713. int ret;
  714. pr_irda("%s called\n", __func__);
  715. fpga_enable(1, 1);
  716. client->addr = IRDA_I2C_ADDR;
  717. ret = i2c_master_recv(client, buf_ir_test, READ_LENGTH);
  718. if (ret < 0)
  719. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  720. pr_irda("%s: buf_ir dev_id: 0x%02x, 0x%02x\n", __func__,
  721. buf_ir_test[2], buf_ir_test[3]);
  722. ret = data->dev_id = (buf_ir_test[2] << 8 | buf_ir_test[3]);
  723. fpga_enable(0, 0);
  724. return ret;
  725. }
  726. /* sysfs node check_ir */
  727. static ssize_t check_ir_show(struct device *dev, struct device_attribute *attr,
  728. char *buf)
  729. {
  730. struct irda_ice40_data *data = dev_get_drvdata(dev);
  731. int ret;
  732. ret = irda_read_device_info(data);
  733. return snprintf(buf, 4, "%d\n", ret);
  734. }
  735. /* sysfs node irda_test */
  736. static ssize_t irda_test_store(struct device *dev,
  737. struct device_attribute *attr, const char *buf, size_t size)
  738. {
  739. int ret, i;
  740. struct irda_ice40_data *data = dev_get_drvdata(dev);
  741. struct i2c_client *client = data->client;
  742. struct {
  743. unsigned char addr;
  744. unsigned char data[IRDA_TEST_CODE_SIZE-1];
  745. } i2c_block_transfer;
  746. unsigned char BSR_data[IRDA_TEST_CODE_SIZE-1] = {
  747. 0x00, 0x8D, 0x00, 0x96, 0x00, 0x00, 0xAD, 0x00,
  748. 0xAB, 0x00, 0x18, 0x00, 0x3E, 0x00, 0x18, 0x00,
  749. 0x3D, 0x00, 0x18, 0x00, 0x3E, 0x00, 0x18, 0x00,
  750. 0x11, 0x00, 0x18, 0x00, 0x11, 0x00, 0x18, 0x00,
  751. 0x12, 0x00, 0x18, 0x00, 0x11, 0x00, 0x18, 0x00,
  752. 0x13, 0x00, 0x16, 0x00, 0x3E, 0x00, 0x18, 0x00,
  753. 0x3D, 0x00, 0x18, 0x00, 0x3E, 0x00, 0x18, 0x00,
  754. 0x13, 0x00, 0x16, 0x00, 0x11, 0x00, 0x19, 0x00,
  755. 0x11, 0x00, 0x18, 0x00, 0x11, 0x00, 0x18, 0x00,
  756. 0x11, 0x00, 0x18, 0x00, 0x3E, 0x00, 0x18, 0x00,
  757. 0x3D, 0x00, 0x19, 0x00, 0x3D, 0x00, 0x18, 0x00,
  758. 0x11, 0x00, 0x18, 0x00, 0x13, 0x00, 0x17, 0x00,
  759. 0x11, 0x00, 0x18, 0x00, 0x11, 0x00, 0x18, 0x00,
  760. 0x14, 0x00, 0x16, 0x00, 0x11, 0x00, 0x18, 0x00,
  761. 0x11, 0x00, 0x18, 0x00, 0x11, 0x00, 0x19, 0x00,
  762. 0x3D, 0x00, 0x18, 0x00, 0x3E, 0x00, 0x18, 0x00,
  763. 0x3D, 0x00, 0x18, 0x00, 0x3E, 0x00, 0x18, 0x00,
  764. 0x3D, 0x00, 0x18, 0x07, 0x58, 0x42, 0xCF
  765. };
  766. pr_irda("IRDA test code start\n");
  767. /* change address for IRDA */
  768. client->addr = IRDA_I2C_ADDR;
  769. /* make data for sending */
  770. for (i = 0; i < IRDA_TEST_CODE_SIZE - 1; i++)
  771. i2c_block_transfer.data[i] = BSR_data[i];
  772. fpga_enable(1, 1);
  773. /* sending data by I2C */
  774. i2c_block_transfer.addr = IRDA_TEST_CODE_ADDR;
  775. ret = i2c_master_send(client, (unsigned char *) &i2c_block_transfer,
  776. IRDA_TEST_CODE_SIZE);
  777. if (ret < 0) {
  778. pr_err("%s: err1 %d\n", __func__, ret);
  779. ret = i2c_master_send(client,
  780. (unsigned char *) &i2c_block_transfer, IRDA_TEST_CODE_SIZE);
  781. if (ret < 0)
  782. pr_err("%s: err2 %d\n", __func__, ret);
  783. }
  784. fpga_enable(0, 0);
  785. return size;
  786. }
  787. static ssize_t irda_test_show(struct device *dev, struct device_attribute *attr,
  788. char *buf)
  789. {
  790. return strlen(buf);
  791. }
  792. #ifdef IRDA_RX_ENABLE
  793. static int irda_learn_mode(struct irda_ice40_data *data)
  794. {
  795. struct i2c_client *client = data->client;
  796. int ret;
  797. unsigned char _data[2];
  798. pr_irda("%s[%d] Set operation learning mode\n",
  799. __func__, __LINE__);
  800. client->addr = IRDA_I2C_ADDR;
  801. _data[0] = IRDA_REG_OPERATION;
  802. _data[1] = data->operation = IRDA_LEARN;
  803. if (!Is_clk_enabled)
  804. fpga_enable(1, 1);
  805. ret = i2c_master_send(client, (unsigned char *)_data, sizeof(_data));
  806. if (ret < 0) {
  807. pr_irda("%s client address error1\n", __func__);
  808. ret = i2c_master_send(client,
  809. (unsigned char *)_data, sizeof(_data));
  810. if (ret < 0)
  811. pr_irda("%s client address error2\n", __func__);
  812. }
  813. return ret;
  814. }
  815. static int irda_stop_mode(struct irda_ice40_data *data)
  816. {
  817. struct i2c_client *client = data->client;
  818. int ret;
  819. unsigned char _data[2];
  820. pr_irda("%s[%d] Set operation stop mode\n",
  821. __func__, __LINE__);
  822. client->addr = IRDA_I2C_ADDR;
  823. _data[0] = IRDA_REG_OPERATION;
  824. _data[1] = data->operation = IRDA_STOP;
  825. if (!Is_clk_enabled)
  826. fpga_enable(1, 1);
  827. ret = i2c_master_send(client, (unsigned char *)_data, sizeof(_data));
  828. if (ret < 0) {
  829. pr_irda("%s client address error1\n", __func__);
  830. ret = i2c_master_send(client,
  831. (unsigned char *)_data, sizeof(_data));
  832. if (ret < 0)
  833. pr_irda("%s client address error2\n", __func__);
  834. }
  835. if (Is_clk_enabled)
  836. fpga_enable(0, 0);
  837. return ret;
  838. }
  839. static ssize_t irda_learn_store(struct device *dev,
  840. struct device_attribute *attr, const char *buf, size_t size)
  841. {
  842. struct irda_ice40_data *data = dev_get_drvdata(dev);
  843. int ret;
  844. ret = irda_learn_mode(data);
  845. if (ret < 0)
  846. pr_err("%s failed set irda learning mode\n", __func__);
  847. return size;
  848. }
  849. static ssize_t irda_learn_show(struct device *dev,
  850. struct device_attribute *attr, char *buf)
  851. {
  852. int i;
  853. char *bufp = buf;
  854. for (i = 0; i < sizeof(learning_buf); i++) {
  855. bufp += snprintf(bufp, SNPRINT_BUF_SIZE,
  856. "%x,", learning_buf[i]);
  857. learning_buf[i] = 0xFF;
  858. }
  859. return strlen(buf);
  860. }
  861. static ssize_t irda_test_uevent(struct device *dev,
  862. struct device_attribute *attr, const char *buf, size_t size)
  863. {
  864. struct irda_ice40_data *data = dev_get_drvdata(dev);
  865. unsigned int mode = 0;
  866. int ret;
  867. pr_irda("ir_receive called %s\n", __func__);
  868. ret = sscanf(buf, "%d", &mode);
  869. if (ret == 0) {
  870. dev_err(&data->client->dev, "fail to get mode.\n");
  871. return size;
  872. }
  873. switch_set_state(&switch_irda_receive, mode);
  874. pr_irda("switch_set_state call %s\n", __func__);
  875. return size;
  876. }
  877. unsigned char rx_buf[1024];
  878. static ssize_t irda_get_rx(struct device *dev, struct device_attribute *attr,
  879. char *buf)
  880. {
  881. struct irda_ice40_data *data = dev_get_drvdata(dev);
  882. struct i2c_client *client = data->client;
  883. int i;
  884. char *bufp = buf;
  885. if (!Is_clk_enabled)
  886. fpga_enable(1, 1);
  887. irda_ice40_read(client, IRDA_I2C_RX_ADDR, 0x00, sizeof(rx_buf), rx_buf);
  888. for (i = 0; i < sizeof(rx_buf); i++) {
  889. if ((rx_buf[i] == 0) && (rx_buf[i+1] == 0)) {
  890. if (i > 1)
  891. break;
  892. }
  893. bufp += snprintf(bufp, SNPRINT_BUF_SIZE, "%x,", rx_buf[i]);
  894. rx_buf[i] = 0xFF;
  895. }
  896. bufp += snprintf(bufp, SNPRINT_BUF_SIZE, "cnt %d\n", i);
  897. if (Is_clk_enabled)
  898. fpga_enable(0, 0);
  899. return strlen(buf);
  900. }
  901. static ssize_t irda_set_stop(struct device *dev,
  902. struct device_attribute *attr, const char *buf, size_t size)
  903. {
  904. struct irda_ice40_data *data = dev_get_drvdata(dev);
  905. int ret;
  906. ret = irda_stop_mode(data);
  907. if (ret < 0)
  908. pr_err("%s failed set irda stop mode\n", __func__);
  909. return size;
  910. }
  911. static irqreturn_t irda_irq_handler(int irq, void *devid)
  912. {
  913. struct irda_ice40_data *dev = devid;
  914. struct i2c_client *client = dev->client;
  915. int ret, size;
  916. int i;
  917. if ((dev->operation & IRDA_LEARN) != IRDA_LEARN) {
  918. pr_irda("%s[%d] Operation not learning mode\n",
  919. __func__, __LINE__);
  920. return 0;
  921. }
  922. if (!Is_clk_enabled)
  923. fpga_enable(1, 1);
  924. udelay(200);
  925. irda_ice40_read(client, IRDA_I2C_RX_ADDR, 0x00,
  926. sizeof(learning_buf), learning_buf);
  927. ret = irda_stop_mode(dev);
  928. if (ret < 0)
  929. pr_err("%s failed set irda stop mode\n", __func__);
  930. if (Is_clk_enabled)
  931. fpga_enable(0, 0);
  932. pr_irda("%s IRQ Handle End\n", __func__);
  933. for (i = 0; i < sizeof(learning_buf); i++) {
  934. if ((learning_buf[i] == 0) && (learning_buf[i+1] == 0)) {
  935. if (i > 1)
  936. break;
  937. }
  938. }
  939. size = dev->learn_cnt = i;
  940. switch_set_state(&switch_irda_receive, size);
  941. return 0;
  942. }
  943. #endif
  944. static struct device_attribute ice40_attrs[] = {
  945. __ATTR(ice40_fpga_fw_update, S_IRUGO|S_IWUSR|S_IWGRP,
  946. ice40_fpga_fw_update_show, ice40_fpga_fw_update_store),
  947. __ATTR(ice40_ver_check, S_IRUGO|S_IWUSR|S_IWGRP,
  948. ice40_ver_check_show, NULL),
  949. #ifdef IRDA_RX_ENABLE
  950. __ATTR(ir_receive, S_IRUGO|S_IWUSR|S_IWGRP, NULL, irda_test_uevent),
  951. __ATTR(ir_learn, S_IRUGO|S_IWUSR|S_IWGRP,
  952. irda_learn_show, irda_learn_store),
  953. __ATTR(ir1, S_IRUGO|S_IWUSR|S_IWGRP, irda_get_rx, irda_set_stop),
  954. #endif
  955. __ATTR(check_ir, S_IRUGO|S_IWUSR|S_IWGRP, check_ir_show, NULL),
  956. __ATTR(ir_send, S_IRUGO|S_IWUSR|S_IWGRP, remocon_show, remocon_store),
  957. __ATTR(ir_send_result, S_IRUGO|S_IWUSR|S_IWGRP, remocon_ack, NULL),
  958. __ATTR(irda_test, S_IRUGO|S_IWUSR|S_IWGRP,
  959. irda_test_show, irda_test_store)
  960. };
  961. static int ice40_open(struct inode *inode, struct file *file)
  962. {
  963. int err = 0;
  964. pr_irda("ice40_open %s\n", __func__);
  965. err = nonseekable_open(inode, file);
  966. if (err)
  967. return err;
  968. file->private_data = g_data;
  969. return 0;
  970. }
  971. static int ice40_close(struct inode *inode, struct file *file)
  972. {
  973. pr_irda("ice40_close %s\n", __func__);
  974. return 0;
  975. }
  976. static void store_pattern(struct irda_ice40_data **data,
  977. int pattern[], int length)
  978. {
  979. int i;
  980. int count;
  981. (*data)->i2c_block_transfer.addr = 0x00;
  982. /* operation cmd */
  983. (*data)->i2c_block_transfer.data[2] = (*data)->operation & 0x03;
  984. /* frequency cmd 15~8 */
  985. (*data)->i2c_block_transfer.data[3] = ((*data)->ir_freq >> 8) & 0xFF;
  986. /* frequency cmd 7~0 */
  987. (*data)->i2c_block_transfer.data[4] = (*data)->ir_freq & 0xFF;
  988. count = 5;
  989. for (i = 0; i < length; i++) {
  990. (*data)->ir_sum += pattern[i];
  991. (*data)->i2c_block_transfer.data[count++] = pattern[i] >> 8;
  992. (*data)->i2c_block_transfer.data[count++] = pattern[i] & 0xFF;
  993. }
  994. (*data)->count = count;
  995. }
  996. static long ice40_ioctl(struct file *file, unsigned int cmd,
  997. unsigned long arg)
  998. {
  999. struct irda_ice40_data *data = file->private_data;
  1000. pr_irda("ice40 ioctl %s\n", __func__);
  1001. switch (cmd) {
  1002. case IR_IOCTL_SET_FREQ:
  1003. {
  1004. int freq = (int)arg;
  1005. if (freq < 0) {
  1006. pr_irda("Improper data for frequency\n");
  1007. return -EINVAL;
  1008. }
  1009. pr_irda("SET_FREQ cmd %d\n", freq);
  1010. data->ir_freq = freq;
  1011. data->operation = (freq >> 16) & 0x03;
  1012. pr_irda("SET_OPERATION cmd %d\n", data->operation);
  1013. break;
  1014. }
  1015. case IR_IOCTL_SET_SIZE:
  1016. {
  1017. int size = (int)arg;
  1018. if (size < 0) {
  1019. pr_irda("Re-enter pattern size\n");
  1020. return -EINVAL;
  1021. }
  1022. pr_irda("SET_SIZE cmd %d\n", size);
  1023. data->length = size;
  1024. break;
  1025. }
  1026. case IR_IOCTL_SET_DATA:
  1027. {
  1028. int *pattern;
  1029. if (data->ir_freq == 0) {
  1030. pr_irda("ir_freq is NOT set\n");
  1031. return -EIO;
  1032. }
  1033. if (data->length == 0) {
  1034. pr_irda("pattern size is NOT set\n");
  1035. return -EIO;
  1036. }
  1037. if (data->operation > IRDA_REPEAT) {
  1038. pr_irda("pattern operation is wrong set\n");
  1039. return -EIO;
  1040. }
  1041. pattern = kmalloc(((data->length)*sizeof(int)),
  1042. GFP_KERNEL);
  1043. if (!pattern)
  1044. return -ENOMEM;
  1045. if (copy_from_user(pattern, (int *)arg,
  1046. (sizeof(int)*(data->length)))) {
  1047. pr_irda("Re-enter the pattern array\n");
  1048. kfree(pattern);
  1049. return -EINVAL;
  1050. }
  1051. pr_irda("SET_DATA cmd\n");
  1052. pr_irda("1st / 2nd value : %d, %d\n",
  1053. pattern[0], pattern[1]);
  1054. store_pattern(&data, pattern, data->length);
  1055. kfree(pattern);
  1056. break;
  1057. }
  1058. case IR_IOCTL_START:
  1059. {
  1060. if (data->ir_freq == 0) {
  1061. pr_irda("ir_freq is NOT set\n");
  1062. return -EIO;
  1063. }
  1064. if (data->operation == 0xFFFF) {
  1065. pr_irda("pattern operation is NOT set\n");
  1066. return -EIO;
  1067. }
  1068. if (data->count == 0) {
  1069. pr_irda("transmission Data is NOT set\n");
  1070. return -EIO;
  1071. }
  1072. return ir_remocon_work(data, data->count);
  1073. }
  1074. case IR_IOCTL_STOP:
  1075. {
  1076. break;
  1077. }
  1078. #ifdef IRDA_RX_ENABLE
  1079. case IR_IOCTL_GET_LEARN:
  1080. {
  1081. int ret = 0;
  1082. pr_irda("Send learning value\n");
  1083. ret = copy_to_user((char *)arg,
  1084. learning_buf, data->learn_cnt);
  1085. if (ret < 0)
  1086. pr_err("%s failed copy_to_user %d\n",
  1087. __func__, ret);
  1088. data->learn_cnt = 0;
  1089. break;
  1090. }
  1091. case IR_IOCTL_OPERATION:
  1092. {
  1093. int ret;
  1094. int operation = (int)arg;
  1095. if (operation < 0) {
  1096. pr_irda("Re-enter pattern operation\n");
  1097. return -EINVAL;
  1098. }
  1099. data->operation = operation;
  1100. if (data->operation == IRDA_LEARN) {
  1101. pr_irda("ir_learn operation%d\n", operation);
  1102. ret = irda_learn_mode(data);
  1103. } else if (data->operation == IRDA_STOP) {
  1104. pr_irda("ir_stop operation%d\n", operation);
  1105. ret = irda_stop_mode(data);
  1106. } else
  1107. pr_irda("ir_single or repeat operation%d\n",
  1108. operation);
  1109. break;
  1110. }
  1111. #endif
  1112. default:
  1113. {
  1114. pr_irda("Unknown CMD\n");
  1115. return -ENOTTY;
  1116. }
  1117. }
  1118. return 0;
  1119. }
  1120. static const struct file_operations ice40_fops = {
  1121. .owner = THIS_MODULE,
  1122. .open = ice40_open,
  1123. .release = ice40_close,
  1124. .unlocked_ioctl = ice40_ioctl,
  1125. };
  1126. static int ice40_power_onoff(struct i2c_client *client, int onoff)
  1127. {
  1128. static struct regulator *fpga_vcc3p3;
  1129. int error;
  1130. fpga_vcc3p3 = regulator_get(&client->dev, "max77826_ldo15");
  1131. if (IS_ERR(fpga_vcc3p3)) {
  1132. pr_err("%s: could not get vdda vreg, rc=%ld\n",
  1133. __func__, PTR_ERR(fpga_vcc3p3));
  1134. return PTR_ERR(fpga_vcc3p3);
  1135. }
  1136. error = regulator_set_voltage(fpga_vcc3p3,
  1137. 3300000, 3300000);
  1138. if (error)
  1139. pr_err("%s: error fpga_vcc3p3 set voltage ret=%d\n",
  1140. __func__, error);
  1141. error = regulator_enable(fpga_vcc3p3);
  1142. if (error)
  1143. pr_err("%s: error fpga_vcc3p3 enabling regulator\n", __func__);
  1144. pr_irda("%s setting gpio config.\n", __func__);
  1145. return error;
  1146. }
  1147. static int __devinit irda_ice40_probe(struct i2c_client *client,
  1148. const struct i2c_device_id *id)
  1149. {
  1150. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  1151. struct irda_ice40_data *data;
  1152. struct irda_ice40_platform_data *pdata;
  1153. struct device *irda_ice40_dev;
  1154. int i, error, ret;
  1155. pr_irda("%s probe!\n", __func__);
  1156. enable_counte = 0;
  1157. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  1158. return -EIO;
  1159. if (client->dev.of_node) {
  1160. pdata = devm_kzalloc(&client->dev,
  1161. sizeof(struct irda_ice40_platform_data),
  1162. GFP_KERNEL);
  1163. if (!pdata) {
  1164. dev_err(&client->dev, "Failed to allocate memory\n");
  1165. return -ENOMEM;
  1166. }
  1167. error = irda_ice40_parse_dt(&client->dev, pdata);
  1168. if (error)
  1169. return error;
  1170. } else
  1171. pdata = client->dev.platform_data;
  1172. g_pdata = pdata;
  1173. irda_ice40_config();
  1174. if (g_pdata->fw_ver == 1) {
  1175. ret = ice40_power_onoff(client, POWER_ON);
  1176. if (ret) {
  1177. dev_err(&client->dev, "%s\n", __func__);
  1178. return ret;
  1179. }
  1180. }
  1181. client->irq = gpio_to_irq(pdata->irda_irq);
  1182. data = kzalloc(sizeof(struct irda_ice40_data), GFP_KERNEL);
  1183. if (NULL == data) {
  1184. pr_err("Failed to data allocate %s\n", __func__);
  1185. error = -ENOMEM;
  1186. goto err_free_mem;
  1187. }
  1188. data->client = client;
  1189. mutex_init(&en_mutex);
  1190. mutex_init(&data->mutex);
  1191. data->ir_sum = 0;
  1192. data->operation = 0xFFFF;
  1193. data->count = 0;
  1194. #ifdef IRDA_RX_ENABLE
  1195. data->learn_cnt = 0;
  1196. #endif
  1197. i2c_set_clientdata(client, data);
  1198. #ifdef IRDA_RX_ENABLE
  1199. ret = switch_dev_register(&switch_irda_receive);
  1200. if (ret < 0) {
  1201. dev_err(&client->dev, "Failed to switch_dev_register\n");
  1202. error = ret;
  1203. goto err_switch_dev;
  1204. }
  1205. ret = request_threaded_irq(client->irq,
  1206. NULL, irda_irq_handler,
  1207. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  1208. "irda-irq", data);
  1209. if (ret) {
  1210. pr_err("failed to request irq %d\n",
  1211. client->irq);
  1212. goto err_free_mem;
  1213. }
  1214. #endif
  1215. g_data = data;
  1216. /* IOCTL Add */
  1217. data->miscdev.minor = MISC_DYNAMIC_MINOR;
  1218. data->miscdev.name = IR_DRIVER_NAME;
  1219. data->miscdev.fops = &ice40_fops;
  1220. data->miscdev.parent = &client->dev;
  1221. ret = misc_register(&data->miscdev);
  1222. if (ret < 0) {
  1223. dev_err(&client->dev, "Device misc_register failed\n");
  1224. error = ret;
  1225. goto err_misc;
  1226. }
  1227. irda_ice40_dev = device_create(sec_class, NULL, 0, data, "sec_ir");
  1228. if (IS_ERR(irda_ice40_dev))
  1229. pr_err("Failed to create irda_ice40_dev device in sec_ir\n");
  1230. /* sysfs entries */
  1231. for (i = 0; i < ARRAY_SIZE(ice40_attrs); i++) {
  1232. if (device_create_file(irda_ice40_dev, &ice40_attrs[i]) < 0)
  1233. pr_err("Failed to create device file(%s)!\n",
  1234. ice40_attrs[i].attr.name);
  1235. }
  1236. /*Create dedicated thread so that
  1237. the delay of our work does not affect others*/
  1238. data->firmware_dl =
  1239. create_singlethread_workqueue("ice40_firmware_dl");
  1240. INIT_DELAYED_WORK(&data->fw_dl, fw_work);
  1241. /* min 1ms is needed */
  1242. queue_delayed_work(data->firmware_dl,
  1243. &data->fw_dl, msecs_to_jiffies(20));
  1244. pr_irda("%s complete[%d]\n", __func__, __LINE__);
  1245. return 0;
  1246. err_misc:
  1247. pr_err("probe misc resister failed %s\n", __func__);
  1248. #ifdef IRDA_RX_ENABLE
  1249. switch_dev_unregister(&switch_irda_receive);
  1250. err_switch_dev:
  1251. pr_err("probe switch_dev_resister failed %s\n", __func__);
  1252. #endif
  1253. err_free_mem:
  1254. kfree(data);
  1255. return error;
  1256. }
  1257. static int __devexit irda_ice40_remove(struct i2c_client *client)
  1258. {
  1259. struct irda_ice40_data *data = i2c_get_clientdata(client);
  1260. i2c_set_clientdata(client, NULL);
  1261. #ifdef IRDA_RX_ENABLE
  1262. switch_dev_unregister(&switch_irda_receive);
  1263. #endif
  1264. misc_deregister(&data->miscdev);
  1265. kfree(data);
  1266. return 0;
  1267. }
  1268. static const struct i2c_device_id irda_ice40_id[] = {
  1269. {"irda_ice40", 0},
  1270. {}
  1271. };
  1272. MODULE_DEVICE_TABLE(i2c, barcode_id);
  1273. #ifdef CONFIG_OF
  1274. static struct of_device_id irda_ice40_match_table[] = {
  1275. { .compatible = "irda_ice40",},
  1276. { },
  1277. };
  1278. #else
  1279. #define irda_ice40_match_table NULL
  1280. #endif
  1281. static struct i2c_driver ice40_i2c_driver = {
  1282. .driver = {
  1283. .name = "irda_ice40",
  1284. .owner = THIS_MODULE,
  1285. .of_match_table = irda_ice40_match_table,
  1286. },
  1287. .probe = irda_ice40_probe,
  1288. .remove = __devexit_p(irda_ice40_remove),
  1289. .id_table = irda_ice40_id,
  1290. };
  1291. static int __init irda_ice40_init(void)
  1292. {
  1293. pr_irda("%s\n", __func__);
  1294. return i2c_add_driver(&ice40_i2c_driver);
  1295. }
  1296. module_init(irda_ice40_init);
  1297. static void __exit irda_ice40_exit(void)
  1298. {
  1299. i2c_del_driver(&ice40_i2c_driver);
  1300. }
  1301. module_exit(irda_ice40_exit);
  1302. MODULE_LICENSE("GPL");
  1303. MODULE_DESCRIPTION("SEC IrDA");