light_ring.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/fs.h>
  5. #include <linux/cdev.h>
  6. #include <linux/device.h>
  7. #include <linux/uaccess.h>
  8. #include <linux/slab.h>
  9. #include <linux/pci.h>
  10. #include <linux/i2c.h>
  11. #include <linux/acpi.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/wait.h>
  14. #include <linux/sched.h>
  15. #include <linux/poll.h>
  16. #include <linux/mutex.h>
  17. #include <linux/delay.h>
  18. #include <linux/sched.h>
  19. #include <linux/timer.h>
  20. #include <linux/workqueue.h>
  21. #define PCA9685_ADDR 0x40
  22. #define PCA9685_DEFAULT_ADDR 0x40
  23. #define PCA9685_GENERAL_CALL 0x00
  24. #define PCA9685_MODE1 0x00
  25. #define PCA9685_MODE2 0x01
  26. #define PCA9685_SUBADR1 0x02
  27. #define PCA9685_SUBADR2 0x03
  28. #define PCA9685_SUBADR3 0x04
  29. #define PCA9685_ALLCALLADR 0x05
  30. #define PCA9685_LED0_ON_L 0x06
  31. #define PCA9685_LED0_ON_H 0x07
  32. #define PCA9685_LED0_OFF_L 0x08
  33. #define PCA9685_LED0_OFF_H 0x09
  34. #define PCA9685_ALL_LED_ON_L 0xFA
  35. #define PCA9685_ALL_LED_ON_H 0xFB
  36. #define PCA9685_ALL_LED_OFF_L 0xFC
  37. #define PCA9685_ALL_LED_OFF_H 0xFD
  38. #define PCA9685_PRESCALE 0xFE
  39. #define PCA9685_TESTMODE 0xFF
  40. #define MODE1_RESTART 0x80
  41. #define MODE1_EXTCLK 0x40
  42. #define MODE1_AI 0x20
  43. #define MODE1_SLEEP 0x10
  44. #define MODE1_SUB1 0x08
  45. #define MODE1_SUB2 0x04
  46. #define MODE1_SUB3 0x02
  47. #define MODE1_ALLCALL 0x01
  48. #define MODE2_INVRT 0x10
  49. #define MODE2_OCH 0x08
  50. #define MODE2_OUTDRV 0x04
  51. #define MODE2_OUTNE1 0x02
  52. #define MODE2_OUTNE0 0x01
  53. #define PCA9685_PWM_RESOLUTION 4096
  54. #define PCA9685_OSC_FREQ 25000000
  55. #define PCA9685_MIN_FREQ 24
  56. #define PCA9685_MAX_FREQ 1526
  57. #define PCA9685_OK 0
  58. #define PCA9685_ERR_OPEN -1
  59. #define PCA9685_ERR_ADDR -2
  60. #define PCA9685_ERR_WRITE -3
  61. #define PCA9685_ERR_READ -4
  62. #define PCA9685_ERR_PARAM -5
  63. #define PCA9685_ERR_INIT -6
  64. #define COLOR_RED 0xff0000 //{255, 0, 0}
  65. #define COLOR_GREEN 0x00ff00 //{0, 255, 0}
  66. #define COLOR_BLUE 0x0000ff //{0, 0, 255}
  67. #define COLOR_WHITE 0xffffff //{255, 255, 255}
  68. #define COLOR_FUCHSIA 0xff00ff //{255, 0, 255}
  69. #define COLOR_YELLOW 0xffff00 //{255, 255, 0}
  70. #define COLOR_CYAN 0x00ffff //{0, 255, 255}
  71. #define LIGHT_MODE_OFF 0x01
  72. #define LIGHT_MODE_ON 0x02
  73. #define LIGHT_MODE_FLASH_1SEC 0x03
  74. #define LIGHT_MODE_FLASH_2SEC 0x04
  75. #define LIGHT_MODE_FLASH_3SEC 0x05
  76. #define LIGHT_MODE_FLASH_SLOW 0x06
  77. #define LIGHT_MODE_FLASH_MEDIUM 0x07
  78. #define LIGHT_MODE_FLASH_FAST 0x08
  79. struct lightring_data
  80. {
  81. u32 brightness;
  82. u32 color;
  83. u32 max_fade_brightness;
  84. u32 mode;
  85. u32 flash_time;
  86. };
  87. struct light_ring_i2c_dev
  88. {
  89. struct pci_dev *pdev;
  90. struct i2c_adapter *adapter;
  91. struct i2c_client client;
  92. struct cdev cdev;
  93. struct class *class;
  94. struct device *device;
  95. dev_t dev_num;
  96. struct mutex lock;
  97. int bus_number;
  98. struct lightring_data *lightring;
  99. struct delayed_work delay_work1;
  100. };
  101. int set_color(unsigned int color);
  102. static struct light_ring_i2c_dev *global_dev = NULL;
  103. static void delay_work_func(struct work_struct *work)
  104. {
  105. static int flag = 0;
  106. printk("this is delay_work_func !\n");
  107. if(global_dev->lightring->flash_time != 0)
  108. {
  109. if(global_dev->lightring->mode == LIGHT_MODE_FLASH_1SEC || \
  110. global_dev->lightring->mode == LIGHT_MODE_FLASH_2SEC || \
  111. global_dev->lightring->mode == LIGHT_MODE_FLASH_3SEC)
  112. {
  113. if(flag == 0)
  114. {
  115. set_color(global_dev->lightring->color);
  116. flag = 1;
  117. }
  118. else
  119. {
  120. set_color(0);
  121. flag = 0;
  122. }
  123. }
  124. else if(global_dev->lightring->mode == LIGHT_MODE_FLASH_SLOW || \
  125. global_dev->lightring->mode == LIGHT_MODE_FLASH_MEDIUM || \
  126. global_dev->lightring->mode == LIGHT_MODE_FLASH_FAST)
  127. {
  128. if(global_dev->lightring->brightness >= 100)
  129. {
  130. global_dev->lightring->brightness = 1;
  131. }
  132. global_dev->lightring->brightness += global_dev->lightring->brightness;
  133. if(global_dev->lightring->brightness >= 100)
  134. {
  135. global_dev->lightring->brightness = 100;
  136. }
  137. set_color(global_dev->lightring->color);
  138. }
  139. schedule_delayed_work(&global_dev->delay_work1, msecs_to_jiffies(global_dev->lightring->flash_time));
  140. }
  141. }
  142. // s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value);
  143. // s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command);
  144. int set_color(unsigned int color)
  145. {
  146. int i = 0;
  147. int ret = 0;
  148. unsigned char red;
  149. unsigned char green;
  150. unsigned char blue;
  151. unsigned char red_pwm_l = 0;
  152. unsigned char red_pwm_h = 0;
  153. unsigned char green_pwm_l = 0;
  154. unsigned char green_pwm_h = 0;
  155. unsigned char blue_pwm_l = 0;
  156. unsigned char blue_pwm_h = 0;
  157. red = 255 - (((color >> 16) & 0xff) * global_dev->lightring->brightness / 100);
  158. green = 255 - (((color >> 8) & 0xff) * global_dev->lightring->brightness / 100);
  159. blue = 255 - ((color & 0xff) * global_dev->lightring->brightness / 100);
  160. red_pwm_l = (red * 4095 / 255) & 0xff;
  161. red_pwm_h = ((red * 4095 / 255) >> 8) & 0x0f;
  162. green_pwm_l = (green * 4095 / 255) & 0xff;
  163. green_pwm_h = ((green * 4095 / 255) >> 8) & 0x0f;
  164. blue_pwm_l = (blue * 4095 / 255) & 0xff;
  165. blue_pwm_h = ((blue * 4095 / 255) >> 8) & 0x0f;
  166. for (i = 0; i < 3; i++)
  167. {
  168. ret |= i2c_smbus_write_byte_data(&global_dev->client, 0x06 + i * 12, 0);
  169. ret |= i2c_smbus_write_byte_data(&global_dev->client, 0x07 + i * 12, 0);
  170. ret |= i2c_smbus_write_byte_data(&global_dev->client, 0x08 + i * 12, red_pwm_l);
  171. ret |= i2c_smbus_write_byte_data(&global_dev->client, 0x09 + i * 12, red_pwm_h);
  172. ret |= i2c_smbus_write_byte_data(&global_dev->client, 0x0a + i * 12, 0);
  173. ret |= i2c_smbus_write_byte_data(&global_dev->client, 0x0b + i * 12, 0);
  174. ret |= i2c_smbus_write_byte_data(&global_dev->client, 0x0c + i * 12, green_pwm_l);
  175. ret |= i2c_smbus_write_byte_data(&global_dev->client, 0x0d + i * 12, green_pwm_h);
  176. ret |= i2c_smbus_write_byte_data(&global_dev->client, 0x0e + i * 12, 0);
  177. ret |= i2c_smbus_write_byte_data(&global_dev->client, 0x0f + i * 12, 0);
  178. ret |= i2c_smbus_write_byte_data(&global_dev->client, 0x10 + i * 12, blue_pwm_l);
  179. ret |= i2c_smbus_write_byte_data(&global_dev->client, 0x11 + i * 12, blue_pwm_h);
  180. }
  181. if(ret != 0)
  182. {
  183. printk("set color failed\n");
  184. return -1;
  185. }
  186. return 0;
  187. }
  188. static ssize_t brightness_show(struct kobject *kobj, struct kobj_attribute *attr,
  189. char *buf)
  190. {
  191. return sprintf(buf, "%u\n", global_dev->lightring->brightness);
  192. }
  193. static ssize_t brightness_store(struct kobject *kobj, struct kobj_attribute *attr,
  194. const char *buf, size_t count)
  195. {
  196. u32 val;
  197. int ret;
  198. ret = kstrtou32(buf, 10, &val);
  199. if (ret < 0)
  200. {
  201. printk("Lightring: brightness format error\n");
  202. return ret;
  203. }
  204. if(val > 100)
  205. {
  206. printk("Lightring: brightness out of range\n");
  207. return -1;
  208. }
  209. global_dev->lightring->brightness = val;
  210. set_color(global_dev->lightring->color);
  211. pr_info("Lightring: brightness set to %u\n", val);
  212. return count;
  213. }
  214. static struct kobj_attribute brightness_attr =
  215. __ATTR(brightness, 0644, brightness_show, brightness_store);
  216. /* ==================== color ==================== */
  217. static ssize_t color_show(struct kobject *kobj, struct kobj_attribute *attr,
  218. char *buf)
  219. {
  220. return sprintf(buf, "0x%06x\n", global_dev->lightring->color);
  221. }
  222. static ssize_t color_store(struct kobject *kobj, struct kobj_attribute *attr,
  223. const char *buf, size_t count)
  224. {
  225. u32 val;
  226. int ret;
  227. if (strncmp(buf, "White", 5) == 0)
  228. {
  229. printk("Lightring: color set to White\n");
  230. val = COLOR_WHITE;
  231. }
  232. else if (strncmp(buf, "Red", 3) == 0)
  233. {
  234. printk("Lightring: color set to Red\n");
  235. val = COLOR_RED;
  236. }
  237. else if (strncmp(buf, "Green", 5) == 0)
  238. {
  239. printk("Lightring: color set to Green\n");
  240. val = COLOR_GREEN;
  241. }
  242. else if (strncmp(buf, "Blue", 4) == 0)
  243. {
  244. printk("Lightring: color set to Blue\n");
  245. val = COLOR_BLUE;
  246. }
  247. else if (strncmp(buf, "Fuchsia", 7) == 0)
  248. {
  249. printk("Lightring: color set to Fuchsia\n");
  250. val = COLOR_FUCHSIA;
  251. }
  252. else if (strncmp(buf, "Yellow", 6) == 0)
  253. {
  254. printk("Lightring: color set to Yellow\n");
  255. val = COLOR_YELLOW;
  256. }
  257. else if (strncmp(buf, "Cyan", 4) == 0)
  258. {
  259. printk("Lightring: color set to Cyan\n");
  260. val = COLOR_CYAN;
  261. }
  262. else
  263. {
  264. printk("Lightring: color format error\n");
  265. return -1;
  266. }
  267. global_dev->lightring->color = val;
  268. ret = set_color(val);
  269. if (ret < 0)
  270. {
  271. pr_err("Lightring: set color failed\n");
  272. return ret;
  273. }
  274. pr_info("Lightring: color set to 0x%06x\n", val);
  275. return count;
  276. }
  277. static struct kobj_attribute color_attr =
  278. __ATTR(color, 0644, color_show, color_store);
  279. /* ==================== max_fade_brightness ==================== */
  280. static ssize_t max_fade_brightness_show(struct kobject *kobj,
  281. struct kobj_attribute *attr,
  282. char *buf)
  283. {
  284. return sprintf(buf, "%u\n", global_dev->lightring->max_fade_brightness);
  285. }
  286. static ssize_t max_fade_brightness_store(struct kobject *kobj,
  287. struct kobj_attribute *attr,
  288. const char *buf, size_t count)
  289. {
  290. u32 val;
  291. int ret;
  292. ret = kstrtou32(buf, 10, &val);
  293. if (ret < 0)
  294. return ret;
  295. global_dev->lightring->max_fade_brightness = val;
  296. pr_info("Lightring: max_fade_brightness set to %u\n", val);
  297. return count;
  298. }
  299. static struct kobj_attribute max_fade_brightness_attr =
  300. __ATTR(max_fade_brightness, 0644, max_fade_brightness_show,
  301. max_fade_brightness_store);
  302. /* ==================== mode ==================== */
  303. static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr,
  304. char *buf)
  305. {
  306. const char *mode_str;
  307. switch (global_dev->lightring->mode)
  308. {
  309. case 0:
  310. mode_str = "static";
  311. break;
  312. case 1:
  313. mode_str = "breathing";
  314. break;
  315. case 2:
  316. mode_str = "rainbow";
  317. break;
  318. case 3:
  319. mode_str = "fade";
  320. break;
  321. default:
  322. mode_str = "unknown";
  323. break;
  324. }
  325. return sprintf(buf, "%s (%u)\n", mode_str, global_dev->lightring->mode);
  326. }
  327. static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
  328. const char *buf, size_t count)
  329. {
  330. u32 val;
  331. int ret;
  332. int mode = 0;
  333. int time = 0;
  334. if (strncmp(buf, "off", 3) == 0)
  335. {
  336. printk("Lightring: mode set to off\n");
  337. mode = LIGHT_MODE_OFF;
  338. time = 0;
  339. }
  340. else if (strncmp(buf, "on", 2) == 0)
  341. {
  342. printk("Lightring: mode set to on\n");
  343. mode = LIGHT_MODE_ON;
  344. time = 0;
  345. }
  346. else if (strncmp(buf, "flash_1sec", 10) == 0)
  347. {
  348. printk("Lightring: mode set to flash_1sec\n");
  349. mode = LIGHT_MODE_FLASH_1SEC;
  350. time = 1000;
  351. }
  352. else if (strncmp(buf, "flash_2sec", 10) == 0)
  353. {
  354. printk("Lightring: mode set to flash_2sec\n");
  355. mode = LIGHT_MODE_FLASH_2SEC;
  356. time = 2000;
  357. }
  358. else if (strncmp(buf, "flash_3sec", 10) == 0)
  359. {
  360. printk("Lightring: mode set to flash_3sec\n");
  361. mode = LIGHT_MODE_FLASH_3SEC;
  362. time = 3000;
  363. }
  364. else if (strncmp(buf, "fade_slow", 9) == 0)
  365. {
  366. printk("Lightring: mode set to flash_slow\n");
  367. mode = LIGHT_MODE_FLASH_SLOW;
  368. time = 400;
  369. }
  370. else if (strncmp(buf, "fade_medium", 11) == 0)
  371. {
  372. printk("Lightring: mode set to flash_medium\n");
  373. mode = LIGHT_MODE_FLASH_MEDIUM;
  374. time = 200;
  375. }
  376. else if (strncmp(buf, "fade_fast", 9) == 0)
  377. {
  378. printk("Lightring: mode set to flash_fast\n");
  379. mode = LIGHT_MODE_FLASH_FAST;
  380. time = 50;
  381. }
  382. else
  383. {
  384. printk("Lightring: mode format error\n");
  385. return -1;
  386. }
  387. global_dev->lightring->mode = mode;
  388. global_dev->lightring->flash_time = time;
  389. if(time != 0)
  390. {
  391. schedule_delayed_work(&global_dev->delay_work1, msecs_to_jiffies(global_dev->lightring->flash_time));
  392. }
  393. pr_info("global_dev->lightring: mode set to %u :flash_time=%d\n", global_dev->lightring->mode, global_dev->lightring->flash_time);
  394. return count;
  395. }
  396. static struct kobj_attribute mode_attr =
  397. __ATTR(mode, 0644, mode_show, mode_store);
  398. static struct attribute *lightring_attrs[] = {
  399. &brightness_attr.attr,
  400. &color_attr.attr,
  401. &max_fade_brightness_attr.attr,
  402. &mode_attr.attr,
  403. NULL,
  404. };
  405. static struct attribute_group lightring_attr_group = {
  406. .attrs = lightring_attrs,
  407. };
  408. // static struct kobject *vfiec_kobj;
  409. extern struct kobject *vfiec_kobj;
  410. static struct kobject *lightring_kobj;
  411. static struct i2c_adapter *find_i2c_adapter_by_pci(struct pci_dev *pdev)
  412. {
  413. struct i2c_adapter *adapter = NULL;
  414. struct device *dev;
  415. struct fwnode_handle *fwnode;
  416. if (!pdev)
  417. return NULL;
  418. fwnode = dev_fwnode(&pdev->dev);
  419. if (!fwnode)
  420. {
  421. pr_err("PCI device has no fwnode\n");
  422. return NULL;
  423. }
  424. struct device_link *link;
  425. list_for_each_entry(link, &pdev->dev.links.consumers, s_node)
  426. {
  427. if (link->supplier == &pdev->dev)
  428. {
  429. if (link->consumer->type &&
  430. link->consumer->type->name &&
  431. strcmp(link->consumer->type->name, "i2c_adapter") == 0)
  432. {
  433. adapter = i2c_verify_adapter(link->consumer);
  434. if (adapter)
  435. break;
  436. }
  437. }
  438. }
  439. if (adapter)
  440. {
  441. i2c_put_adapter(adapter);
  442. adapter = i2c_get_adapter(adapter->nr);
  443. printk("i2c_get_adapter\n");
  444. return adapter;
  445. }
  446. int i;
  447. for (i = 0; i < 255; i++)
  448. {
  449. adapter = i2c_get_adapter(i);
  450. if (adapter)
  451. {
  452. if (adapter->dev.parent == &pdev->dev)
  453. {
  454. return adapter;
  455. }
  456. i2c_put_adapter(adapter);
  457. }
  458. }
  459. return NULL;
  460. }
  461. static struct pci_dev *init_pci_device(void)
  462. {
  463. struct pci_dev *pdev;
  464. pdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0x1f, 4));
  465. if (!pdev)
  466. {
  467. pr_err("Failed to find PCI device 0000:00:1f.4\n");
  468. return NULL;
  469. }
  470. pr_info("Found PCI device: %04x:%04x\n", pdev->vendor, pdev->device);
  471. return pdev;
  472. }
  473. int pca9685_all_off(void)
  474. {
  475. if (i2c_smbus_write_byte_data(&global_dev->client, PCA9685_ALL_LED_ON_L, 0) < 0)
  476. return PCA9685_ERR_WRITE;
  477. if (i2c_smbus_write_byte_data(&global_dev->client, PCA9685_ALL_LED_ON_H, 0) < 0)
  478. return PCA9685_ERR_WRITE;
  479. if (i2c_smbus_write_byte_data(&global_dev->client, PCA9685_ALL_LED_OFF_L, 0) < 0)
  480. return PCA9685_ERR_WRITE;
  481. if (i2c_smbus_write_byte_data(&global_dev->client, PCA9685_ALL_LED_OFF_H, 0x10) < 0)
  482. return PCA9685_ERR_WRITE;
  483. return PCA9685_OK;
  484. }
  485. int pca9685_set_open_drain(bool enable)
  486. {
  487. uint8_t mode2;
  488. int ret;
  489. ret = i2c_smbus_read_byte_data(&global_dev->client, PCA9685_MODE2);
  490. if (ret < 0)
  491. return PCA9685_ERR_READ;
  492. mode2 = ret & 0xff;
  493. if (enable)
  494. mode2 &= ~MODE2_OUTDRV;
  495. else
  496. mode2 |= MODE2_OUTDRV;
  497. return i2c_smbus_write_byte_data(&global_dev->client, PCA9685_MODE2, mode2);
  498. }
  499. int pca9685_set_pwm_freq(int freq)
  500. {
  501. int ret = 0;
  502. uint8_t prescale, old_mode, new_mode;
  503. int prescale_val = (PCA9685_OSC_FREQ / (4096 * freq)) - 1;
  504. prescale = (uint8_t)(prescale_val);
  505. if (prescale < 3)
  506. prescale = 3;
  507. ret = i2c_smbus_read_byte_data(&global_dev->client, PCA9685_MODE1);
  508. if (ret < 0)
  509. return PCA9685_ERR_READ;
  510. old_mode = ret & 0xff;
  511. new_mode = (old_mode & ~MODE1_RESTART) | MODE1_SLEEP;
  512. if (i2c_smbus_write_byte_data(&global_dev->client, PCA9685_MODE1, new_mode) < 0)
  513. return PCA9685_ERR_WRITE;
  514. if (i2c_smbus_write_byte_data(&global_dev->client, PCA9685_PRESCALE, prescale) < 0)
  515. return PCA9685_ERR_WRITE;
  516. if (i2c_smbus_write_byte_data(&global_dev->client, PCA9685_MODE1, old_mode) < 0)
  517. return PCA9685_ERR_WRITE;
  518. udelay(5000);
  519. if (i2c_smbus_write_byte_data(&global_dev->client, PCA9685_MODE1, old_mode | MODE1_RESTART | MODE1_AI) < 0)
  520. {
  521. return PCA9685_ERR_WRITE;
  522. }
  523. return 0;
  524. }
  525. void init_pca9685(void)
  526. {
  527. int ret = 0;
  528. // reset
  529. ret |= i2c_smbus_write_byte_data(&global_dev->client, 0x00, 0x06);
  530. udelay(10000);
  531. ret |= i2c_smbus_write_byte_data(&global_dev->client, PCA9685_MODE2, MODE2_OUTDRV | MODE2_OCH);
  532. ret |= i2c_smbus_write_byte_data(&global_dev->client, PCA9685_MODE1, MODE1_RESTART | MODE1_AI | MODE1_ALLCALL);
  533. printk("init_pca9685: %d\n", ret);
  534. ret = pca9685_set_pwm_freq(50);
  535. if (ret != 0)
  536. {
  537. printk("pca9685_set_pwm_freq failed: %d\n", ret);
  538. }
  539. ret = pca9685_all_off();
  540. if (ret != 0)
  541. {
  542. printk("pca9685_all_off failed: %d\n", ret);
  543. }
  544. ret = pca9685_set_open_drain(true);
  545. if (ret != 0)
  546. {
  547. printk("pca9685_set_open_drain failed: %d\n", ret);
  548. }
  549. }
  550. int light_ring_init(void)
  551. {
  552. int ret;
  553. struct i2c_board_info info;
  554. struct light_ring_i2c_dev *dev;
  555. pr_info("light_ring_i2c_dev initializing...\n");
  556. dev = kzalloc(sizeof(struct light_ring_i2c_dev), GFP_KERNEL);
  557. if (!dev)
  558. {
  559. pr_err("Failed to allocate device structure\n");
  560. return -ENOMEM;
  561. }
  562. global_dev = dev;
  563. mutex_init(&dev->lock);
  564. dev->pdev = init_pci_device();
  565. if (!dev->pdev)
  566. {
  567. pr_err("Failed to initialize PCI device\n");
  568. ret = -ENODEV;
  569. goto err_free_dev;
  570. }
  571. dev->adapter = find_i2c_adapter_by_pci(dev->pdev);
  572. if (!dev->adapter)
  573. {
  574. pr_err("Failed to find I2C adapter\n");
  575. ret = -ENODEV;
  576. goto err_put_pci;
  577. }
  578. dev->bus_number = dev->adapter->nr;
  579. pr_info("Found I2C adapter on bus %d\n", dev->bus_number);
  580. if (!i2c_check_functionality(dev->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA |
  581. I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  582. {
  583. dev_err(&dev->adapter->dev, "Adapter does not support required SMBus features\n");
  584. return -ENOTSUPP;
  585. }
  586. else
  587. {
  588. pr_info("Adapter supports required SMBus features\n");
  589. }
  590. dev->client.adapter = dev->adapter;
  591. dev->client.addr = PCA9685_DEFAULT_ADDR;
  592. dev->client.flags = 0;
  593. INIT_DELAYED_WORK(&dev->delay_work1, delay_work_func);
  594. init_pca9685();
  595. global_dev->lightring = kzalloc(sizeof(*global_dev->lightring), GFP_KERNEL);
  596. if (!global_dev->lightring)
  597. {
  598. pr_err("Failed to allocate lightring structure\n");
  599. ret = -ENOMEM;
  600. goto err_put_pci;
  601. }
  602. global_dev->lightring->brightness = 60;
  603. global_dev->lightring->color = 0xFFFFFF;
  604. global_dev->lightring->max_fade_brightness = 255;
  605. global_dev->lightring->mode = 0; /* static */
  606. lightring_kobj = kobject_create_and_add("lightring", vfiec_kobj);
  607. if (!lightring_kobj)
  608. {
  609. ret = -ENOMEM;
  610. goto err_free;
  611. }
  612. ret = sysfs_create_group(lightring_kobj, &lightring_attr_group);
  613. if (ret)
  614. {
  615. pr_err("Failed to create sysfs group: %d\n", ret);
  616. goto err_lightring;
  617. }
  618. return 0;
  619. err_lightring:
  620. kobject_put(lightring_kobj);
  621. // err_vfiec:
  622. // kobject_put(vfiec_kobj);
  623. err_free:
  624. kfree(global_dev->lightring);
  625. err_put_pci:
  626. pci_dev_put(dev->pdev);
  627. err_free_dev:
  628. kfree(dev);
  629. global_dev = NULL;
  630. return ret;
  631. }
  632. void light_ring_exit(void)
  633. {
  634. struct light_ring_i2c_dev *dev = global_dev;
  635. global_dev->lightring->flash_time = 0;
  636. cancel_delayed_work_sync(&global_dev->delay_work1);
  637. sysfs_remove_group(lightring_kobj, &lightring_attr_group);
  638. kobject_put(lightring_kobj);
  639. // kobject_put(vfiec_kobj);
  640. kfree(global_dev->lightring);
  641. printk(KERN_INFO "LED module unloaded\n");
  642. if (dev->adapter)
  643. i2c_put_adapter(dev->adapter);
  644. if (dev->pdev)
  645. pci_dev_put(dev->pdev);
  646. kfree(dev);
  647. global_dev = NULL;
  648. }