irda_common_ldo.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /*
  2. * driver/irda IRDA 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. * Compile this driver when MC96FR116 is powered by LDO shared with other peripherals.
  22. * LDO control in this case is only for IRLED power (3.3V)
  23. * Vendor: ABOV Semiconductor
  24. * Part Name: MC96FR116CU
  25. * Vendor Contact: byoungsu.jeong@abov.co.kr, youngdo.yi@abov.co.kr, wooyoung.lee@abov.co.kr
  26. * Driver modelled on ir_remote_mc96fr116.c
  27. *
  28. * Revision History:
  29. * # Author Description
  30. * 1 jayant.s47 Basic Bringup for Kmini ATT
  31. * 2 jayant.s47 Increase delay for I2C read success in Kmini 3G
  32. * 3 jayant.s47 Kmini ATT bringup: Modifications in fw_update to align it
  33. * with vendor specifications. Optimize fw_update routine by removing useless delays.
  34. *************************************************************************************************************
  35. */
  36. #include <linux/kernel.h>
  37. #include <linux/module.h>
  38. #include <linux/err.h>
  39. #include <linux/i2c.h>
  40. #include <linux/irq.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/platform_device.h>
  43. #include <linux/slab.h>
  44. #include <linux/delay.h>
  45. #include <linux/workqueue.h>
  46. #include <linux/device.h>
  47. #include <linux/ir_remote_con_mc96.h>
  48. #include <linux/regulator/consumer.h>
  49. #if defined (CONFIG_OF)
  50. #include <linux/of_device.h>
  51. #include <linux/of_gpio.h>
  52. #endif
  53. #include <linux/earlysuspend.h>
  54. #include "irda_fw_version202.h"
  55. #include "irda_fw_version103.h"
  56. #include "irda_fw_version104.h"
  57. #include <mach/gpio.h>
  58. #include <linux/ir_remote_con_mc96.h>
  59. #define MAX_SIZE 2048
  60. #define MC96_READ_LENGTH 8
  61. #define DUMMY 0xffff
  62. #define MC96FR116C_0x101 0x101
  63. #define MC96FR116C_0x103 0x103
  64. //#define DEBUG 1
  65. struct ir_remocon_data {
  66. struct mutex mutex;
  67. struct i2c_client *client;
  68. struct mc96_platform_data *pdata;
  69. struct early_suspend early_suspend;
  70. char signal[MAX_SIZE];
  71. int length;
  72. int count;
  73. int dev_id;
  74. int ir_freq;
  75. int ir_sum;
  76. int on_off;
  77. };
  78. #ifdef CONFIG_HAS_EARLYSUSPEND
  79. static void ir_remocon_early_suspend(struct early_suspend *h);
  80. static void ir_remocon_late_resume(struct early_suspend *h);
  81. #endif
  82. static int count_number;
  83. static int ack_number;
  84. static int download_pass;
  85. static int update_retry_count;
  86. #define MAX_UPDATE_RETRY 5
  87. #define TEST_ONLY 0 /* Enable this only to test when ADB isn't available */
  88. #if TEST_ONLY
  89. static void irda_remocon_work(struct ir_remocon_data *ir_data, int count);
  90. static void test_store(struct ir_remocon_data *data, const char *buf) {
  91. int i;
  92. data->pdata->ir_wake_en(data->pdata,0);
  93. udelay(200);
  94. data->pdata->ir_wake_en(data->pdata,1);
  95. msleep(30);
  96. data->ir_freq = 38400;
  97. data->signal[2] = 0x00;
  98. data->count = 3;
  99. for(i = 0; i < 138; i++)
  100. data->signal[data->count++] = buf[i];
  101. irda_remocon_work(data, data->count);
  102. }
  103. static void irda_test_probe(struct ir_remocon_data *data) {
  104. /* Send IRDA byte sequence for volume up */
  105. unsigned char test[] = {
  106. 0x96,0x00,0x00,0xAD,0x00,0xAB,0x00,0x18,0x00,0x3E,0x00,0x18,
  107. 0x00,0x3D,0x00,0x18,0x00,0x3E,0x00,0x18,0x00,0x11,0x00,0x18,
  108. 0x00,0x11,0x00,0x18,0x00,0x12,0x00,0x18,0x00,0x11,0x00,0x18,
  109. 0x00,0x13,0x00,0x16,0x00,0x3E,0x00,0x18,0x00,0x3D,0x00,0x18,
  110. 0x00,0x3E,0x00,0x18,0x00,0x13,0x00,0x16,0x00,0x11,0x00,0x19,
  111. 0x00,0x11,0x00,0x18,0x00,0x11,0x00,0x18,0x00,0x11,0x00,0x18,
  112. 0x00,0x3E,0x00,0x18,0x00,0x3D,0x00,0x19,0x00,0x3D,0x00,0x18,
  113. 0x00,0x11,0x00,0x18,0x00,0x13,0x00,0x17,0x00,0x11,0x00,0x18,
  114. 0x00,0x11,0x00,0x18,0x00,0x14,0x00,0x16,0x00,0x11,0x00,0x18,
  115. 0x00,0x11,0x00,0x18,0x00,0x11,0x00,0x19,0x00,0x3D,0x00,0x18,
  116. 0x00,0x3E,0x00,0x18,0x00,0x3D,0x00,0x18,0x00,0x3E,0x00,0x18,
  117. 0x00,0x3D,0x00,0x18,0x07,0x58
  118. };
  119. test_store(data, test);
  120. }
  121. #endif
  122. static void irda_wake_en_func(struct mc96_platform_data *pdata, bool onoff)
  123. {
  124. /*Need to add parser before this*/
  125. gpio_direction_output(pdata->irda_wake_en, onoff);
  126. printk(KERN_ERR "%s: irda_wake_en : %d\n", __func__, onoff);
  127. }
  128. static void irda_gpio_init(struct mc96_platform_data *pdata)
  129. {
  130. pdata->ir_wake_en = irda_wake_en_func;
  131. if(gpio_request(pdata->irda_wake_en, "IRDA Wakeup EN Pin"))
  132. pr_err("%s IRDA Wakeup EN GPIO Request failed\n",__func__);
  133. gpio_tlmm_config(GPIO_CFG(pdata->irda_wake_en, 0, GPIO_CFG_OUTPUT,
  134. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_DISABLE);
  135. gpio_tlmm_config(GPIO_CFG(pdata->irda_poweron, 0, GPIO_CFG_OUTPUT,
  136. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  137. gpio_request(pdata->irda_poweron, "irda_poweron");
  138. gpio_direction_output(pdata->irda_poweron, 1);
  139. if (gpio_request(pdata->irda_irq_gpio, "ira_irq"))
  140. pr_err("%s IRDA LED IRQ GPIO Request failed\n",__func__);
  141. gpio_direction_input(pdata->irda_irq_gpio);
  142. gpio_tlmm_config(GPIO_CFG(pdata->irda_scl_gpio, 0, GPIO_CFG_INPUT,
  143. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_DISABLE);
  144. gpio_tlmm_config(GPIO_CFG(pdata->irda_sda_gpio, 0, GPIO_CFG_INPUT,
  145. GPIO_CFG_NO_PULL, GPIO_CFG_2MA), GPIO_CFG_DISABLE);
  146. }
  147. static int vled_ic_onoff;
  148. static struct regulator *vled_ic;
  149. static int irda_led_onoff(bool onoff)
  150. {
  151. int ret = 0;
  152. if (onoff) {
  153. ret = regulator_set_voltage(vled_ic,3300000,3300000);
  154. if (ret) {
  155. pr_err("%s regulaor set volatge failed\n",__func__);
  156. goto regulator_fail;
  157. }
  158. ret = regulator_enable(vled_ic);
  159. if (ret) {
  160. pr_err("%s regulaor enable failed\n",__func__);
  161. goto regulator_fail;
  162. }
  163. vled_ic_onoff = 1;
  164. printk(KERN_CRIT "%s Regulator On!\n",__func__);
  165. } else if (vled_ic_onoff == 1) {
  166. ret = regulator_disable(vled_ic);
  167. if (ret) {
  168. pr_err("%s regulaor disable failed\n",__func__);
  169. goto regulator_fail;
  170. }
  171. vled_ic_onoff = 0;
  172. printk(KERN_CRIT "%s Regulator OFF!\n",__func__);
  173. }
  174. return ret;
  175. regulator_fail:
  176. regulator_put(vled_ic);
  177. return ret;
  178. }
  179. #define FW_RW_RETRY 2
  180. static int irda_fw_update(struct ir_remocon_data *ir_data)
  181. {
  182. struct ir_remocon_data *data = ir_data;
  183. struct i2c_client *client = data->client;
  184. int i=0, ret = 0, frame_count = 0;
  185. u8 buf_ir_test[8];
  186. const u8 *IRDA_fw;
  187. const u8 calc_chksum[] = {0x3A, 0x02, 0x10, 0x00, 0xF0, 0x20, 0xFF, 0xDF};
  188. IRDA_fw = IRDA_binary_104;
  189. frame_count = FRAME_COUNT_104;
  190. /* Switch on chip in Bootloader mode, wake low */
  191. irda_led_onoff(1);
  192. data->pdata->ir_wake_en(data->pdata, 0);
  193. gpio_set_value(data->pdata->irda_poweron, 1);
  194. gpio_tlmm_config(GPIO_CFG(data->pdata->irda_irq_gpio, 0, GPIO_CFG_INPUT,
  195. GPIO_CFG_PULL_UP, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  196. msleep(150);
  197. for(i = 0; i < FW_RW_RETRY; i++) {
  198. ret = i2c_master_recv(client, buf_ir_test, MC96_READ_LENGTH);
  199. if (ret < 0) {
  200. pr_err(KERN_ERR " %s: err %d\n", __func__, ret);
  201. }
  202. else if((buf_ir_test[0] << 8 | buf_ir_test[1]) == 0x0001) {
  203. printk(KERN_CRIT "%s: Perform checksum calculation next, ret %d\n", __func__,ret);
  204. break;
  205. }
  206. msleep(60);
  207. }
  208. if (i == FW_RW_RETRY) {
  209. printk(KERN_CRIT "%s: Chip not responding, powerdown device\n", __func__);
  210. download_pass = 0;
  211. goto fw_update_end;
  212. }
  213. for(i = 0; i < FW_RW_RETRY; i++) {
  214. msleep(60);
  215. ret = i2c_master_send(client, calc_chksum, MC96_READ_LENGTH);
  216. if(ret < 0)
  217. continue;
  218. else
  219. break;
  220. }
  221. if(i == FW_RW_RETRY) {
  222. printk(KERN_CRIT "%s: checksum calculation fail, ret: %d\n", __func__, ret);
  223. download_pass = 0;
  224. goto fw_update_end;
  225. }
  226. msleep(60);
  227. /* Do a master read before downloading FW and validate device in bootloader mode */
  228. /* Expected sequence if correct FW 0x6E ,0xBA ,0x10 ,0x00 ,0x20 ,0xFF ,0x02 ,0x57 */
  229. /* Try reading twice */
  230. ret = i2c_master_recv(client, buf_ir_test, MC96_READ_LENGTH);
  231. if (ret < 0) {
  232. msleep(50);
  233. ret = i2c_master_recv(client, buf_ir_test, MC96_READ_LENGTH);
  234. if (ret < 0) {
  235. pr_err(KERN_ERR " %s: read fail after csum calc: err %d\n", __func__, ret);
  236. download_pass = 0;
  237. goto fw_update_end;
  238. }
  239. }
  240. #ifdef DEBUG
  241. print_hex_dump(KERN_CRIT, "IRDA Master Rx: ", 16, 1,
  242. DUMP_PREFIX_ADDRESS, buf_ir_test, 8, 1);
  243. #endif
  244. if((buf_ir_test[0] << 8 | buf_ir_test[1]) == 0x6E93) {
  245. printk(KERN_CRIT "%s: irda fw fine, exit now\n", __func__);
  246. download_pass = 1;
  247. goto powerdown_dev;
  248. }
  249. printk(KERN_CRIT "%s: Start download new Firmware\n", __func__);
  250. msleep(100);
  251. /* Start FW download */
  252. for (i = 0; i < frame_count; i++) {
  253. if (i == frame_count-1) {
  254. ret = i2c_master_send(client,
  255. &IRDA_fw[i * 70], 6);
  256. if (ret < 0) {
  257. printk(KERN_CRIT "%s: FW download master send fail\n", __func__);
  258. goto fw_update_end;
  259. }
  260. } else {
  261. ret = i2c_master_send(client,
  262. &IRDA_fw[i * 70], 70);
  263. if (ret < 0) {
  264. printk(KERN_CRIT "%s: FW download master send fail\n", __func__);
  265. goto fw_update_end;
  266. }
  267. }
  268. msleep(60);
  269. }
  270. /* Do a master read after downloading firmware and validate the FW
  271. * Device still in bootmode, expected = 0x6E ,0xBA ,0x10 ,0x00 ,0x20 ,0xFF ,0x02 ,0x57
  272. */
  273. msleep(100);
  274. ret = i2c_master_recv(client, buf_ir_test, MC96_READ_LENGTH);
  275. if (ret < 0)
  276. printk(KERN_ERR "5. %s: err %d\n", __func__, ret);
  277. #ifdef DEBUG
  278. print_hex_dump(KERN_CRIT, "IRDA Master Rx: ", 16, 1,
  279. DUMP_PREFIX_ADDRESS, buf_ir_test, 8, 1);
  280. #endif
  281. ret = buf_ir_test[0] << 8 | buf_ir_test[1];
  282. if (ret == 0x6E93) {
  283. printk(KERN_INFO " IrDA new firmware downloaded\n");
  284. download_pass = 1;
  285. goto powerdown_dev;
  286. } else {
  287. printk(KERN_ERR "%s: FW Checksum fail after update\n", __func__);
  288. }
  289. fw_update_end:
  290. printk(KERN_CRIT "%s: FAIL, power down device, ret = %d\n", __func__, ret);
  291. download_pass = 0;
  292. update_retry_count++;
  293. /* Check for old FW being present at last attempt to update to 1.3 */
  294. if(update_retry_count == (MAX_UPDATE_RETRY - 1)) {
  295. download_pass = 1;
  296. update_retry_count = 0;
  297. printk(KERN_CRIT "%s: using old firmware\n", __func__);
  298. }
  299. powerdown_dev:
  300. data->pdata->ir_wake_en(data->pdata,0);
  301. gpio_set_value(data->pdata->irda_poweron, 0);
  302. irda_led_onoff(0);
  303. data->on_off = 0;
  304. return 0;
  305. }
  306. static void irda_add_checksum_length(struct ir_remocon_data *ir_data, int count)
  307. {
  308. struct ir_remocon_data *data = ir_data;
  309. int i = 0, csum = 0;
  310. #if 0
  311. printk(KERN_INFO "%s: length: %04x\n", __func__, count);
  312. #endif
  313. data->signal[0] = count >> 8;
  314. data->signal[1] = count & 0xff;
  315. while (i < count) {
  316. csum += data->signal[i];
  317. i++;
  318. }
  319. printk(KERN_INFO "%s: checksum: %04x\n", __func__, csum);
  320. data->signal[count] = csum >> 8;
  321. data->signal[count+1] = csum & 0xff;
  322. }
  323. static int irda_read_device_info(struct ir_remocon_data *ir_data)
  324. {
  325. struct ir_remocon_data *data = ir_data;
  326. struct i2c_client *client = data->client;
  327. u8 buf_ir_test[8];
  328. int ret;
  329. printk(KERN_INFO"%s called\n", __func__);
  330. gpio_set_value(data->pdata->irda_poweron, 1);
  331. data->pdata->ir_wake_en(data->pdata,1);
  332. msleep(60);
  333. ret = i2c_master_recv(client, buf_ir_test, MC96_READ_LENGTH);
  334. if (ret < 0)
  335. dev_err(&client->dev, "%s: err %d\n", __func__, ret);
  336. printk(KERN_INFO "%s: buf_ir dev_id: 0x%02x, 0x%02x\n", __func__,
  337. buf_ir_test[2], buf_ir_test[3]);
  338. ret = data->dev_id = (buf_ir_test[2] << 8 | buf_ir_test[3]);
  339. data->pdata->ir_wake_en(data->pdata,0);
  340. gpio_set_value(data->pdata->irda_poweron, 0);
  341. data->on_off = 0;
  342. return 0;
  343. }
  344. static void irda_reset_chip_user(struct ir_remocon_data *data) {
  345. irda_led_onoff(0);
  346. gpio_set_value(data->pdata->irda_poweron, 0);
  347. data->pdata->ir_wake_en(data->pdata,1);
  348. gpio_tlmm_config(GPIO_CFG(data->pdata->irda_irq_gpio, 0, GPIO_CFG_INPUT,
  349. GPIO_CFG_PULL_UP, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  350. udelay(100);
  351. gpio_set_value(data->pdata->irda_poweron, 1);
  352. msleep(150);
  353. irda_led_onoff(1);
  354. }
  355. static void irda_remocon_work(struct ir_remocon_data *ir_data, int count)
  356. {
  357. struct ir_remocon_data *data = ir_data;
  358. struct i2c_client *client = data->client;
  359. int buf_size = count+2;
  360. int ret, retry, ng_retry, sng_retry;
  361. int emission_time;
  362. int ack_pin_onoff;
  363. #if defined(CONFIG_MACH_ATLANTICLTE_ATT) || defined(CONFIG_MACH_ATLANTICLTE_USC)
  364. int sleep_timing;
  365. int end_data;
  366. #endif
  367. #ifdef DEBUG
  368. int i;
  369. u8 buf[8];
  370. #endif
  371. if (count_number >= 100)
  372. count_number = 0;
  373. count_number++;
  374. ng_retry = sng_retry = 0;
  375. data->on_off = 1;
  376. /* Power on in user IR mode */
  377. irda_reset_chip_user(ir_data);
  378. printk(KERN_INFO "%s: total buf_size: %d\n", __func__, buf_size);
  379. #ifdef DEBUG
  380. ret = i2c_master_recv(client, buf, MC96_READ_LENGTH);
  381. if (ret < 0)
  382. printk(KERN_CRIT "%s: I2C read err %d\n", __func__, ret);
  383. print_hex_dump(KERN_CRIT, "irda: IRDA Master Rx: ", 16, 1,
  384. DUMP_PREFIX_ADDRESS, buf, 8, 1);
  385. printk("%s: print stored bytes\n", __func__);
  386. for (i = 0; i < buf_size; i++)
  387. printk(KERN_INFO "0x%02x, ", data->signal[i]);
  388. printk("\n");
  389. #endif
  390. irda_add_checksum_length(data, count);
  391. mutex_lock(&data->mutex);
  392. resend_data:
  393. ret = i2c_master_send(client, data->signal, buf_size);
  394. if (ret < 0) {
  395. dev_err(&client->dev, "%s: err1 %d\n", __func__, ret);
  396. ret = i2c_master_send(client, data->signal, buf_size);
  397. if (ret < 0)
  398. dev_err(&client->dev, "%s: err2 %d\n", __func__, ret);
  399. }
  400. msleep(10);
  401. ack_pin_onoff = 0;
  402. for(retry = 0; retry < 10; retry++) {
  403. if (gpio_get_value(data->pdata->irda_irq_gpio)) {
  404. if(retry == 9) {
  405. ng_retry++;
  406. if(ng_retry < 2) {
  407. irda_reset_chip_user(ir_data);
  408. goto resend_data;
  409. }
  410. printk(KERN_INFO "%s : %d Checksum NG!\n",
  411. __func__, count_number);
  412. }
  413. ack_pin_onoff = 1;
  414. msleep(3);
  415. } else {
  416. printk(KERN_INFO "%s : %d Checksum OK!\n",
  417. __func__, count_number);
  418. ack_pin_onoff = 2;
  419. break;
  420. }
  421. }
  422. ack_number = ack_pin_onoff;
  423. data->count = 2;
  424. #if defined(CONFIG_MACH_ATLANTICLTE_ATT) || defined(CONFIG_MACH_ATLANTICLTE_USC)
  425. end_data = data->signal[count-2] << 8 | data->signal[count-1];
  426. emission_time = (1000 * (data->ir_sum - end_data) / (data->ir_freq)) + 10;
  427. sleep_timing = emission_time - 130;
  428. if (sleep_timing > 0)
  429. usleep(sleep_timing);
  430. #endif
  431. emission_time = \
  432. (1000 * (data->ir_sum) / (data->ir_freq));
  433. if (emission_time > 0)
  434. usleep(emission_time);
  435. printk(KERN_INFO "%s: emission_time = %d\n",
  436. __func__, emission_time);
  437. for(retry = 0; retry < 3; retry++) {
  438. if (gpio_get_value(data->pdata->irda_irq_gpio)) {
  439. printk(KERN_INFO "%s : %d Sending IR OK!\n",
  440. __func__, count_number);
  441. ack_pin_onoff = 4;
  442. break;
  443. } else {
  444. if(retry == 2) {
  445. sng_retry++;
  446. if(sng_retry < 2) {
  447. irda_reset_chip_user(ir_data);
  448. goto resend_data;
  449. }
  450. printk(KERN_INFO "%s : %d Sending IR NG!\n",
  451. __func__, count_number);
  452. }
  453. ack_pin_onoff = 2;
  454. msleep(65);
  455. }
  456. }
  457. mutex_unlock(&data->mutex);
  458. ack_number += ack_pin_onoff;
  459. #ifndef USE_STOP_MODE
  460. data->on_off = 1;
  461. //data->pdata->ir_wake_en(data->pdata,0);
  462. irda_led_onoff(0);
  463. //gpio_set_value(data->pdata->irda_poweron, 0);
  464. #endif
  465. data->ir_freq = 0;
  466. data->ir_sum = 0;
  467. gpio_tlmm_config(GPIO_CFG(ir_data->pdata->irda_irq_gpio, 0, GPIO_CFG_INPUT,
  468. GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  469. }
  470. static ssize_t remocon_store(struct device *dev, struct device_attribute *attr,
  471. const char *buf, size_t size)
  472. {
  473. struct ir_remocon_data *data = dev_get_drvdata(dev);
  474. unsigned int _data;
  475. int count, i, ret;
  476. #ifdef DEBUG
  477. printk(KERN_CRIT "%s irda -- %s\n", __func__, buf);
  478. #endif
  479. ret = 0;
  480. for (i = 0; i < MAX_SIZE; i++) {
  481. if (sscanf(buf++, "%u", &_data) == 1) {
  482. if (_data == 0 || buf == '\0')
  483. break;
  484. if (data->count == 2) {
  485. data->ir_freq = _data;
  486. data->signal[2] = 0x40; // Mode
  487. data->signal[3] = _data >> 16;
  488. data->signal[4] = (_data >> 8) & 0xFF;
  489. data->signal[5] = _data & 0xFF;
  490. data->count += 4;
  491. } else {
  492. data->ir_sum += _data;
  493. count = data->count;
  494. if(_data > 0x7FFF) {
  495. data->signal[count] = _data >> 24;
  496. data->signal[count+1] = _data >> 16;
  497. data->signal[count+2] = _data >> 8;
  498. data->signal[count+3] = _data & 0xFF;
  499. data->count += 4;
  500. } else {
  501. data->signal[count] = _data >> 8;
  502. data->signal[count+1] = _data & 0xFF;
  503. data->count += 2;
  504. }
  505. }
  506. while (_data > 0) {
  507. buf++;
  508. _data /= 10;
  509. }
  510. } else {
  511. break;
  512. }
  513. }
  514. irda_remocon_work(data, data->count);
  515. return size;
  516. }
  517. static ssize_t remocon_show(struct device *dev, struct device_attribute *attr,
  518. char *buf)
  519. {
  520. struct ir_remocon_data *data = dev_get_drvdata(dev);
  521. int i;
  522. char *bufp = buf;
  523. for (i = 5; i < MAX_SIZE - 1; i++) {
  524. if (data->signal[i] == 0 && data->signal[i+1] == 0)
  525. break;
  526. else
  527. bufp += sprintf(bufp, "%u,", data->signal[i]);
  528. }
  529. return strlen(buf);
  530. }
  531. static ssize_t remocon_ack(struct device *dev, struct device_attribute *attr,
  532. char *buf)
  533. {
  534. //struct ir_remocon_data *data = dev_get_drvdata(dev);
  535. printk(KERN_INFO "%s : ack_number = %d\n", __func__, ack_number);
  536. if (ack_number == 6)
  537. return sprintf(buf, "1\n");
  538. else
  539. return sprintf(buf, "0\n");
  540. }
  541. static DEVICE_ATTR(ir_send, 0664, remocon_show, remocon_store);
  542. static DEVICE_ATTR(ir_send_result, 0664, remocon_ack, NULL);
  543. static ssize_t check_ir_show(struct device *dev, struct device_attribute *attr,
  544. char *buf)
  545. {
  546. struct ir_remocon_data *data = dev_get_drvdata(dev);
  547. int ret;
  548. ret = irda_read_device_info(data);
  549. return snprintf(buf, 4, "%d\n", ret);
  550. }
  551. static DEVICE_ATTR(check_ir, 0664, check_ir_show, NULL);
  552. #ifdef CONFIG_OF
  553. static int irda_mc96_parse_dt(struct device *dev, struct mc96_platform_data *pdata)
  554. {
  555. struct device_node *np = dev->of_node;
  556. pdata->irda_irq_gpio = of_get_named_gpio_flags(np, "mc96fr332,irda_irq_gpio",
  557. 0, &pdata->irq_gpio_flags);
  558. if (pdata->irda_irq_gpio < 0) {
  559. pr_err("%s failed to get irda_irq_gpio\n", __func__);
  560. return pdata->irda_irq_gpio;
  561. }
  562. pdata->irda_poweron = of_get_named_gpio_flags(np, "mc96fr332,irda_poweron",
  563. 0, &pdata->poweron_flags);
  564. if (pdata->irda_poweron < 0) {
  565. pr_err("%s failed to get irda_poweron\n", __func__);
  566. return pdata->irda_poweron;
  567. }
  568. pdata->irda_wake_en = of_get_named_gpio_flags(np, "mc96fr332,irda_wake",
  569. 0, &pdata->wake_en_flags);
  570. if (pdata->irda_wake_en < 0) {
  571. pr_err("%s failed to get irda_wake_en\n", __func__);
  572. return pdata->irda_wake_en;
  573. }
  574. pdata->irda_scl_gpio = of_get_named_gpio_flags(np, "mc96fr332,scl-gpio",
  575. 0, &pdata->irda_scl_flags);
  576. if (pdata->irda_scl_gpio < 0) {
  577. pr_err("%s failed to get irda_scl_gpio\n", __func__);
  578. return pdata->irda_wake_en;
  579. }
  580. pdata->irda_sda_gpio = of_get_named_gpio_flags(np, "mc96fr332,sda-gpio",
  581. 0, &pdata->irda_sda_flags);
  582. if (pdata->irda_sda_gpio < 0) {
  583. pr_err("%s failed to get irda_sda_gpio\n", __func__);
  584. return pdata->irda_sda_gpio;
  585. }
  586. pr_info("%s: irq-gpio:%u poweron:%u wake_up:%u irda-scl:%u irda-sda:%u \n", __func__,
  587. pdata->irda_irq_gpio,pdata->irda_poweron,pdata->irda_wake_en,pdata->irda_scl_gpio,pdata->irda_sda_gpio);
  588. return 0;
  589. }
  590. #endif
  591. static int __devinit irda_remocon_probe(struct i2c_client *client,
  592. const struct i2c_device_id *id)
  593. {
  594. struct i2c_adapter *adapter = to_i2c_adapter(client->dev.parent);
  595. struct ir_remocon_data *data;
  596. struct device *ir_remocon_dev;
  597. struct mc96_platform_data *pdata;
  598. int i, error;
  599. int ret;
  600. printk(KERN_INFO "%s start!\n", __func__);
  601. dev_info(&client->dev,"%s:ir_remocon probe called \n",__func__);
  602. if(client->dev.of_node) {
  603. pdata = devm_kzalloc(&client->dev,
  604. sizeof(struct mc96_platform_data), GFP_KERNEL);
  605. if (!pdata) {
  606. dev_err(&client->dev, "Failed to allocate memory \n");
  607. return -ENOMEM;
  608. }
  609. ret = irda_mc96_parse_dt(&client->dev, pdata);
  610. if (ret < 0)
  611. {
  612. dev_err(&client->dev,"Parse failed \n");
  613. return ret;
  614. }
  615. irda_gpio_init(pdata);
  616. } else
  617. pdata = client->dev.platform_data;
  618. if (!pdata)
  619. return -EINVAL;
  620. if (!i2c_check_functionality(adapter, I2C_FUNC_I2C))
  621. return -EIO;
  622. data = kzalloc(sizeof(struct ir_remocon_data), GFP_KERNEL);
  623. if (NULL == data) {
  624. pr_err("Failed to data allocate %s\n", __func__);
  625. error = -ENOMEM;
  626. goto err_free_mem;
  627. }
  628. data->client = client;
  629. if (client->dev.of_node)
  630. {
  631. data->pdata = pdata;
  632. }else
  633. data->pdata = client->dev.platform_data;
  634. mutex_init(&data->mutex);
  635. data->count = 2;
  636. vled_ic = regulator_get(&client->dev, "vled_3.3v");
  637. if (IS_ERR(vled_ic)) {
  638. pr_err("%s could not get regulator vled_3.3v\n",__func__);
  639. error = -EBUSY;
  640. goto err_free_mem;
  641. }
  642. i2c_set_clientdata(client, data);
  643. for (i = 0; i < MAX_UPDATE_RETRY; i++) {
  644. if (download_pass == 1)
  645. break;
  646. irda_fw_update(data);
  647. }
  648. if (download_pass != 1)
  649. goto err_fw_update_fail;
  650. /*
  651. if(irda_led_onoff(0)) {
  652. pr_err("%s Regulator setting failed\n", __func__);
  653. }
  654. // irda_read_device_info(data);
  655. */
  656. ir_remocon_dev = device_create(sec_class, NULL, 0, data, "sec_ir");
  657. #if TEST_ONLY
  658. for(i = 0; i < 10; i++)
  659. irda_test_probe(data);
  660. #endif
  661. data->on_off = 1;
  662. data->pdata->ir_wake_en(data->pdata,1);
  663. gpio_set_value(data->pdata->irda_poweron, 1);
  664. if (IS_ERR(ir_remocon_dev)) {
  665. pr_err("Failed to create ir_remocon_dev device\n");
  666. goto err_fw_update_fail;
  667. }
  668. if (device_create_file(ir_remocon_dev, &dev_attr_ir_send) < 0) {
  669. pr_err("Failed to create device file(%s)!\n",
  670. dev_attr_ir_send.attr.name);
  671. goto err_del_dev;
  672. }
  673. if (device_create_file(ir_remocon_dev, &dev_attr_ir_send_result) < 0) {
  674. pr_err("Failed to create device file(%s)!\n",
  675. dev_attr_ir_send.attr.name);
  676. goto err_del_dev_file_send;
  677. }
  678. if (device_create_file(ir_remocon_dev, &dev_attr_check_ir) < 0) {
  679. pr_err("Failed to create device file(%s)!\n",
  680. dev_attr_check_ir.attr.name);
  681. goto err_del_dev_file_send_result;
  682. }
  683. #ifdef CONFIG_HAS_EARLYSUSPEND
  684. data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
  685. data->early_suspend.suspend = ir_remocon_early_suspend;
  686. data->early_suspend.resume = ir_remocon_late_resume;
  687. register_early_suspend(&data->early_suspend);
  688. #endif
  689. gpio_tlmm_config(GPIO_CFG(data->pdata->irda_irq_gpio, 0, GPIO_CFG_INPUT,
  690. GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA), GPIO_CFG_ENABLE);
  691. return 0;
  692. err_del_dev_file_send_result:
  693. device_remove_file(ir_remocon_dev, &dev_attr_ir_send_result);
  694. err_del_dev_file_send:
  695. device_remove_file(ir_remocon_dev, &dev_attr_ir_send);
  696. err_del_dev:
  697. device_destroy(sec_class,ir_remocon_dev->devt);
  698. err_fw_update_fail:
  699. regulator_put(vled_ic);
  700. err_free_mem:
  701. kfree(data);
  702. return error;
  703. }
  704. #if defined(CONFIG_PM) || defined(CONFIG_HAS_EARLYSUSPEND)
  705. static int ir_remocon_suspend(struct device *dev)
  706. {
  707. struct i2c_client *client = to_i2c_client(dev);
  708. struct ir_remocon_data *data = i2c_get_clientdata(client);
  709. int ret = 0;
  710. gpio_set_value(data->pdata->irda_poweron, 0);
  711. data->on_off = 0;
  712. data->pdata->ir_wake_en(data->pdata,0);
  713. return ret;
  714. }
  715. static int ir_remocon_resume(struct device *dev)
  716. {
  717. return 0;
  718. }
  719. #endif
  720. #ifdef CONFIG_HAS_EARLYSUSPEND
  721. static void ir_remocon_early_suspend(struct early_suspend *h)
  722. {
  723. struct ir_remocon_data *data;
  724. data = container_of(h, struct ir_remocon_data, early_suspend);
  725. ir_remocon_suspend(&data->client->dev);
  726. }
  727. static void ir_remocon_late_resume(struct early_suspend *h)
  728. {
  729. struct ir_remocon_data *data;
  730. data = container_of(h, struct ir_remocon_data, early_suspend);
  731. ir_remocon_resume(&data->client->dev);
  732. }
  733. #endif
  734. #if defined(CONFIG_PM) && !defined(CONFIG_HAS_EARLYSUSPEND)
  735. static const struct dev_pm_ops ir_remocon_pm_ops = {
  736. .suspend = ir_remocon_suspend,
  737. .resume = ir_remocon_resume,
  738. };
  739. #endif
  740. static int __devexit ir_remocon_remove(struct i2c_client *client)
  741. {
  742. struct ir_remocon_data *data = i2c_get_clientdata(client);
  743. mutex_destroy(&data->mutex);
  744. i2c_set_clientdata(client, NULL);
  745. regulator_disable(vled_ic);
  746. regulator_put(vled_ic);
  747. kfree(data);
  748. return 0;
  749. }
  750. static const struct i2c_device_id mc96_id[] = {
  751. {"mc96", 0},
  752. {}
  753. };
  754. MODULE_DEVICE_TABLE(i2c, mc96_id);
  755. static struct of_device_id mc96_i2c_match_table[] = {
  756. { .compatible = "mc96fr332,i2c",},
  757. {},
  758. };
  759. MODULE_DEVICE_TABLE(of, mc96_i2c_match_table);
  760. static struct i2c_driver mc96_i2c_driver = {
  761. .driver = {
  762. .name = "mc96",
  763. .owner = THIS_MODULE,
  764. .of_match_table = mc96_i2c_match_table,
  765. },
  766. .probe = irda_remocon_probe,
  767. .remove = __devexit_p(ir_remocon_remove),
  768. #if defined(CONFIG_PM) && !defined(CONFIG_HAS_EARLYSUSPEND)
  769. //.pm = &ir_remocon_pm_ops,
  770. #endif
  771. .id_table = mc96_id,
  772. };
  773. static int __init ir_remocon_init(void)
  774. {
  775. return i2c_add_driver(&mc96_i2c_driver);
  776. }
  777. module_init(ir_remocon_init);
  778. static void __exit ir_remocon_exit(void)
  779. {
  780. i2c_del_driver(&mc96_i2c_driver);
  781. }
  782. module_exit(ir_remocon_exit);
  783. MODULE_LICENSE("GPL");
  784. MODULE_DESCRIPTION("SEC IR remote controller");