light_ring.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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)) * global_dev->lightring->brightness / 100;
  149. green = (255 - ((color >> 8) & 0xff)) * global_dev->lightring->brightness / 100;
  150. blue = (255 - (color & 0xff)) * global_dev->lightring->brightness / 100;
  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. {
  192. printk("Lightring: brightness format error\n");
  193. return ret;
  194. }
  195. if(val > 100)
  196. {
  197. printk("Lightring: brightness out of range\n");
  198. return -1;
  199. }
  200. /* 硬件操作:设置亮度 */
  201. global_dev->lightring->brightness = val;
  202. set_color(global_dev->lightring->color);
  203. pr_info("Lightring: brightness set to %u\n", val);
  204. return count;
  205. }
  206. static struct kobj_attribute brightness_attr =
  207. __ATTR(brightness, 0644, brightness_show, brightness_store);
  208. /* ==================== color ==================== */
  209. static ssize_t color_show(struct kobject *kobj, struct kobj_attribute *attr,
  210. char *buf)
  211. {
  212. return sprintf(buf, "0x%06x\n", global_dev->lightring->color);
  213. }
  214. static ssize_t color_store(struct kobject *kobj, struct kobj_attribute *attr,
  215. const char *buf, size_t count)
  216. {
  217. u32 val;
  218. int ret;
  219. if (strncmp(buf, "White", 5) == 0)
  220. {
  221. printk("Lightring: color set to White\n");
  222. val = COLOR_WHITE;
  223. }
  224. else if (strncmp(buf, "Red", 3) == 0)
  225. {
  226. printk("Lightring: color set to Red\n");
  227. val = COLOR_RED;
  228. }
  229. else if (strncmp(buf, "Green", 5) == 0)
  230. {
  231. printk("Lightring: color set to Green\n");
  232. val = COLOR_GREEN;
  233. }
  234. else if (strncmp(buf, "Blue", 4) == 0)
  235. {
  236. printk("Lightring: color set to Blue\n");
  237. val = COLOR_BLUE;
  238. }
  239. else if (strncmp(buf, "Fuchsia", 7) == 0)
  240. {
  241. printk("Lightring: color set to Fuchsia\n");
  242. val = COLOR_FUCHSIA;
  243. }
  244. else if (strncmp(buf, "Yellow", 6) == 0)
  245. {
  246. printk("Lightring: color set to Yellow\n");
  247. val = COLOR_YELLOW;
  248. }
  249. else if (strncmp(buf, "Cyan", 4) == 0)
  250. {
  251. printk("Lightring: color set to Cyan\n");
  252. val = COLOR_CYAN;
  253. }
  254. else
  255. {
  256. printk("Lightring: color format error\n");
  257. return -1;
  258. }
  259. global_dev->lightring->color = val;
  260. ret = set_color(val);
  261. if (ret < 0)
  262. {
  263. pr_err("Lightring: set color failed\n");
  264. return ret;
  265. }
  266. pr_info("Lightring: color set to 0x%06x\n", val);
  267. return count;
  268. }
  269. static struct kobj_attribute color_attr =
  270. __ATTR(color, 0644, color_show, color_store);
  271. /* ==================== max_fade_brightness ==================== */
  272. static ssize_t max_fade_brightness_show(struct kobject *kobj,
  273. struct kobj_attribute *attr,
  274. char *buf)
  275. {
  276. return sprintf(buf, "%u\n", global_dev->lightring->max_fade_brightness);
  277. }
  278. static ssize_t max_fade_brightness_store(struct kobject *kobj,
  279. struct kobj_attribute *attr,
  280. const char *buf, size_t count)
  281. {
  282. u32 val;
  283. int ret;
  284. ret = kstrtou32(buf, 10, &val);
  285. if (ret < 0)
  286. return ret;
  287. global_dev->lightring->max_fade_brightness = val;
  288. pr_info("Lightring: max_fade_brightness set to %u\n", val);
  289. return count;
  290. }
  291. static struct kobj_attribute max_fade_brightness_attr =
  292. __ATTR(max_fade_brightness, 0644, max_fade_brightness_show,
  293. max_fade_brightness_store);
  294. /* ==================== mode ==================== */
  295. static ssize_t mode_show(struct kobject *kobj, struct kobj_attribute *attr,
  296. char *buf)
  297. {
  298. const char *mode_str;
  299. switch (global_dev->lightring->mode)
  300. {
  301. case 0:
  302. mode_str = "static";
  303. break;
  304. case 1:
  305. mode_str = "breathing";
  306. break;
  307. case 2:
  308. mode_str = "rainbow";
  309. break;
  310. case 3:
  311. mode_str = "fade";
  312. break;
  313. default:
  314. mode_str = "unknown";
  315. break;
  316. }
  317. return sprintf(buf, "%s (%u)\n", mode_str, global_dev->lightring->mode);
  318. }
  319. static ssize_t mode_store(struct kobject *kobj, struct kobj_attribute *attr,
  320. const char *buf, size_t count)
  321. {
  322. u32 val;
  323. int ret;
  324. int mode = 0;
  325. int time = 0;
  326. if (strncmp(buf, "off", 3) == 0)
  327. {
  328. printk("Lightring: mode set to off\n");
  329. mode = LIGHT_MODE_OFF;
  330. time = 0;
  331. }
  332. else if (strncmp(buf, "on", 2) == 0)
  333. {
  334. printk("Lightring: mode set to on\n");
  335. mode = LIGHT_MODE_ON;
  336. time = 0;
  337. }
  338. else if (strncmp(buf, "flash_1sec", 10) == 0)
  339. {
  340. printk("Lightring: mode set to flash_1sec\n");
  341. mode = LIGHT_MODE_FLASH_1SEC;
  342. time = 1000;
  343. }
  344. else if (strncmp(buf, "flash_2sec", 10) == 0)
  345. {
  346. printk("Lightring: mode set to flash_2sec\n");
  347. mode = LIGHT_MODE_FLASH_2SEC;
  348. time = 2000;
  349. }
  350. else if (strncmp(buf, "flash_3sec", 10) == 0)
  351. {
  352. printk("Lightring: mode set to flash_3sec\n");
  353. mode = LIGHT_MODE_FLASH_3SEC;
  354. time = 3000;
  355. }
  356. else if (strncmp(buf, "flash_slow", 10) == 0)
  357. {
  358. printk("Lightring: mode set to flash_slow\n");
  359. mode = LIGHT_MODE_FLASH_SLOW;
  360. time = 4000;
  361. }
  362. else if (strncmp(buf, "flash_medium", 12) == 0)
  363. {
  364. printk("Lightring: mode set to flash_medium\n");
  365. mode = LIGHT_MODE_FLASH_MEDIUM;
  366. time = 2000;
  367. }
  368. else if (strncmp(buf, "flash_fast", 10) == 0)
  369. {
  370. printk("Lightring: mode set to flash_fast\n");
  371. mode = LIGHT_MODE_FLASH_FAST;
  372. time = 50;
  373. }
  374. else
  375. {
  376. printk("Lightring: mode format error\n");
  377. return -1;
  378. }
  379. global_dev->lightring->mode = mode;
  380. global_dev->lightring->flash_time = time;
  381. if(time != 0)
  382. {
  383. schedule_delayed_work(&global_dev->delay_work1, msecs_to_jiffies(global_dev->lightring->flash_time));
  384. }
  385. pr_info("global_dev->lightring: mode set to %u :flash_time=%d\n", global_dev->lightring->mode, global_dev->lightring->flash_time);
  386. return count;
  387. }
  388. static struct kobj_attribute mode_attr =
  389. __ATTR(mode, 0644, mode_show, mode_store);
  390. /* ==================== 属性组 ==================== */
  391. static struct attribute *lightring_attrs[] = {
  392. &brightness_attr.attr,
  393. &color_attr.attr,
  394. &max_fade_brightness_attr.attr,
  395. &mode_attr.attr,
  396. NULL,
  397. };
  398. static struct attribute_group lightring_attr_group = {
  399. .attrs = lightring_attrs,
  400. };
  401. // static struct kobject *vfiec_kobj;
  402. extern struct kobject *vfiec_kobj;
  403. static struct kobject *lightring_kobj;
  404. static struct i2c_adapter *find_i2c_adapter_by_pci(struct pci_dev *pdev)
  405. {
  406. struct i2c_adapter *adapter = NULL;
  407. struct device *dev;
  408. struct fwnode_handle *fwnode;
  409. if (!pdev)
  410. return NULL;
  411. /* 获取PCI设备的fwnode */
  412. fwnode = dev_fwnode(&pdev->dev);
  413. if (!fwnode)
  414. {
  415. pr_err("PCI device has no fwnode\n");
  416. return NULL;
  417. }
  418. /* 对于内核5.15,需要使用其他方法查找i2c适配器 */
  419. /* 方法A: 通过设备链接查找 */
  420. struct device_link *link;
  421. /* 遍历PCI设备的消费者链接 */
  422. list_for_each_entry(link, &pdev->dev.links.consumers, s_node)
  423. {
  424. if (link->supplier == &pdev->dev)
  425. {
  426. /* 检查消费者是否是i2c适配器 */
  427. if (link->consumer->type &&
  428. link->consumer->type->name &&
  429. strcmp(link->consumer->type->name, "i2c_adapter") == 0)
  430. {
  431. adapter = i2c_verify_adapter(link->consumer);
  432. if (adapter)
  433. break;
  434. }
  435. }
  436. }
  437. if (adapter)
  438. {
  439. i2c_put_adapter(adapter); /* 先释放引用 */
  440. adapter = i2c_get_adapter(adapter->nr); /* 重新获取 */
  441. printk("i2c_get_adapter\n");
  442. return adapter;
  443. }
  444. /* 方法B: 遍历所有i2c适配器 */
  445. int i;
  446. for (i = 0; i < 255; i++)
  447. {
  448. adapter = i2c_get_adapter(i);
  449. if (adapter)
  450. {
  451. /* 检查适配器的父设备是否是我们的PCI设备 */
  452. if (adapter->dev.parent == &pdev->dev)
  453. {
  454. return adapter;
  455. }
  456. i2c_put_adapter(adapter);
  457. }
  458. }
  459. return NULL;
  460. }
  461. /* 初始化PCI设备 */
  462. static struct pci_dev *init_pci_device(void)
  463. {
  464. struct pci_dev *pdev;
  465. /* 查找PCI设备: domain=0, bus=0, devfn=PCI_DEVFN(0x1f, 4) */
  466. pdev = pci_get_domain_bus_and_slot(0, 0, PCI_DEVFN(0x1f, 4));
  467. if (!pdev)
  468. {
  469. pr_err("Failed to find PCI device 0000:00:1f.4\n");
  470. return NULL;
  471. }
  472. pr_info("Found PCI device: %04x:%04x\n", pdev->vendor, pdev->device);
  473. return pdev;
  474. }
  475. int pca9685_all_off(void)
  476. {
  477. if (i2c_smbus_write_byte_data(&global_dev->client, PCA9685_ALL_LED_ON_L, 0) < 0)
  478. return PCA9685_ERR_WRITE;
  479. if (i2c_smbus_write_byte_data(&global_dev->client, PCA9685_ALL_LED_ON_H, 0) < 0)
  480. return PCA9685_ERR_WRITE;
  481. if (i2c_smbus_write_byte_data(&global_dev->client, PCA9685_ALL_LED_OFF_L, 0) < 0)
  482. return PCA9685_ERR_WRITE;
  483. if (i2c_smbus_write_byte_data(&global_dev->client, PCA9685_ALL_LED_OFF_H, 0x10) < 0)
  484. return PCA9685_ERR_WRITE;
  485. return PCA9685_OK;
  486. }
  487. int pca9685_set_open_drain(bool enable)
  488. {
  489. uint8_t mode2;
  490. int ret;
  491. ret = i2c_smbus_read_byte_data(&global_dev->client, PCA9685_MODE2);
  492. if (ret < 0)
  493. return PCA9685_ERR_READ;
  494. mode2 = ret & 0xff;
  495. if (enable)
  496. mode2 &= ~MODE2_OUTDRV;
  497. else
  498. mode2 |= MODE2_OUTDRV;
  499. return i2c_smbus_write_byte_data(&global_dev->client, PCA9685_MODE2, mode2);
  500. }
  501. int pca9685_set_pwm_freq(int freq)
  502. {
  503. int ret = 0;
  504. uint8_t prescale, old_mode, new_mode;
  505. int prescale_val = (PCA9685_OSC_FREQ / (4096 * freq)) - 1;
  506. prescale = (uint8_t)(prescale_val);
  507. if (prescale < 3)
  508. prescale = 3;
  509. ret = i2c_smbus_read_byte_data(&global_dev->client, PCA9685_MODE1);
  510. if (ret < 0)
  511. return PCA9685_ERR_READ;
  512. old_mode = ret & 0xff;
  513. new_mode = (old_mode & ~MODE1_RESTART) | MODE1_SLEEP;
  514. if (i2c_smbus_write_byte_data(&global_dev->client, PCA9685_MODE1, new_mode) < 0)
  515. return PCA9685_ERR_WRITE;
  516. if (i2c_smbus_write_byte_data(&global_dev->client, PCA9685_PRESCALE, prescale) < 0)
  517. return PCA9685_ERR_WRITE;
  518. if (i2c_smbus_write_byte_data(&global_dev->client, PCA9685_MODE1, old_mode) < 0)
  519. return PCA9685_ERR_WRITE;
  520. udelay(5000);
  521. if (i2c_smbus_write_byte_data(&global_dev->client, PCA9685_MODE1, old_mode | MODE1_RESTART | MODE1_AI) < 0)
  522. {
  523. return PCA9685_ERR_WRITE;
  524. }
  525. return 0;
  526. }
  527. void init_pca9685(void)
  528. {
  529. int ret = 0;
  530. // reset
  531. ret |= i2c_smbus_write_byte_data(&global_dev->client, 0x00, 0x06);
  532. udelay(10000);
  533. ret |= i2c_smbus_write_byte_data(&global_dev->client, PCA9685_MODE2, MODE2_OUTDRV | MODE2_OCH);
  534. ret |= i2c_smbus_write_byte_data(&global_dev->client, PCA9685_MODE1, MODE1_RESTART | MODE1_AI | MODE1_ALLCALL);
  535. printk("init_pca9685: %d\n", ret);
  536. ret = pca9685_set_pwm_freq(50);
  537. if (ret != 0)
  538. {
  539. printk("pca9685_set_pwm_freq failed: %d\n", ret);
  540. }
  541. ret = pca9685_all_off();
  542. if (ret != 0)
  543. {
  544. printk("pca9685_all_off failed: %d\n", ret);
  545. }
  546. ret = pca9685_set_open_drain(true);
  547. if (ret != 0)
  548. {
  549. printk("pca9685_set_open_drain failed: %d\n", ret);
  550. }
  551. }
  552. int light_ring_init(void)
  553. {
  554. int ret;
  555. struct i2c_board_info info;
  556. struct light_ring_i2c_dev *dev;
  557. pr_info("light_ring_i2c_dev initializing...\n");
  558. /* 分配设备结构 */
  559. dev = kzalloc(sizeof(struct light_ring_i2c_dev), GFP_KERNEL);
  560. if (!dev)
  561. {
  562. pr_err("Failed to allocate device structure\n");
  563. return -ENOMEM;
  564. }
  565. global_dev = dev;
  566. mutex_init(&dev->lock);
  567. /* 1. 初始化PCI设备 */
  568. dev->pdev = init_pci_device();
  569. if (!dev->pdev)
  570. {
  571. pr_err("Failed to initialize PCI device\n");
  572. ret = -ENODEV;
  573. goto err_free_dev;
  574. }
  575. /* 2. 通过PCI设备查找I2C适配器 */
  576. dev->adapter = find_i2c_adapter_by_pci(dev->pdev);
  577. if (!dev->adapter)
  578. {
  579. pr_err("Failed to find I2C adapter\n");
  580. ret = -ENODEV;
  581. goto err_put_pci;
  582. }
  583. /* 保存总线编号用于调试 */
  584. dev->bus_number = dev->adapter->nr;
  585. pr_info("Found I2C adapter on bus %d\n", dev->bus_number);
  586. // 检查适配器是否支持字节数据读写
  587. if (!i2c_check_functionality(dev->adapter, I2C_FUNC_SMBUS_READ_BYTE_DATA |
  588. I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  589. {
  590. dev_err(&dev->adapter->dev, "Adapter does not support required SMBus features\n");
  591. return -ENOTSUPP; // 返回不支持错误码
  592. }
  593. else
  594. {
  595. pr_info("Adapter supports required SMBus features\n");
  596. }
  597. /* 初始化I2C客户端 */
  598. dev->client.adapter = dev->adapter; // 你已有的 adapter
  599. dev->client.addr = PCA9685_DEFAULT_ADDR; // 从设备地址
  600. dev->client.flags = 0; // 7位地址模式
  601. INIT_DELAYED_WORK(&dev->delay_work1, delay_work_func);
  602. init_pca9685();
  603. global_dev->lightring = kzalloc(sizeof(*global_dev->lightring), GFP_KERNEL);
  604. if (!global_dev->lightring)
  605. {
  606. pr_err("Failed to allocate lightring structure\n");
  607. ret = -ENOMEM;
  608. goto err_put_pci;
  609. }
  610. /* 默认值 */
  611. global_dev->lightring->brightness = 60;
  612. global_dev->lightring->color = 0xFFFFFF; /* 白色 */
  613. global_dev->lightring->max_fade_brightness = 255;
  614. global_dev->lightring->mode = 0; /* static */
  615. /* 创建 /sys/kernel/vfiec */
  616. // vfiec_kobj = kobject_create_and_add("vfiec", kernel_kobj);
  617. // if (!vfiec_kobj)
  618. // {
  619. // ret = -ENOMEM;
  620. // goto err_free;
  621. // }
  622. /* 创建 /sys/kernel/vfiec/lightring */
  623. lightring_kobj = kobject_create_and_add("lightring", vfiec_kobj);
  624. if (!lightring_kobj)
  625. {
  626. ret = -ENOMEM;
  627. goto err_free;
  628. }
  629. /* 创建属性文件 */
  630. ret = sysfs_create_group(lightring_kobj, &lightring_attr_group);
  631. if (ret)
  632. {
  633. pr_err("Failed to create sysfs group: %d\n", ret);
  634. goto err_lightring;
  635. }
  636. return 0;
  637. err_lightring:
  638. kobject_put(lightring_kobj);
  639. // err_vfiec:
  640. // kobject_put(vfiec_kobj);
  641. err_free:
  642. kfree(global_dev->lightring);
  643. err_put_pci:
  644. pci_dev_put(dev->pdev);
  645. err_free_dev:
  646. kfree(dev);
  647. global_dev = NULL;
  648. return ret;
  649. }
  650. void light_ring_exit(void)
  651. {
  652. struct light_ring_i2c_dev *dev = global_dev;
  653. global_dev->lightring->flash_time = 0;
  654. cancel_delayed_work_sync(&global_dev->delay_work1);
  655. sysfs_remove_group(lightring_kobj, &lightring_attr_group);
  656. kobject_put(lightring_kobj);
  657. // kobject_put(vfiec_kobj);
  658. kfree(global_dev->lightring);
  659. printk(KERN_INFO "LED module unloaded\n");
  660. if (dev->adapter)
  661. i2c_put_adapter(dev->adapter);
  662. if (dev->pdev)
  663. pci_dev_put(dev->pdev);
  664. kfree(dev);
  665. global_dev = NULL;
  666. }