light_ring.c 21 KB

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