light_ring.c 23 KB

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