seg-led-gpio.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for a 7-segment LED display
  4. *
  5. * The decimal point LED present on some devices is currently not
  6. * supported.
  7. *
  8. * Copyright (C) Allied Telesis Labs
  9. */
  10. #include <linux/bitmap.h>
  11. #include <linux/container_of.h>
  12. #include <linux/errno.h>
  13. #include <linux/gpio/consumer.h>
  14. #include <linux/map_to_7segment.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/types.h>
  19. #include <linux/workqueue.h>
  20. #include "line-display.h"
  21. struct seg_led_priv {
  22. struct linedisp linedisp;
  23. struct delayed_work work;
  24. struct gpio_descs *segment_gpios;
  25. };
  26. static void seg_led_update(struct work_struct *work)
  27. {
  28. struct seg_led_priv *priv = container_of(work, struct seg_led_priv, work.work);
  29. struct linedisp *linedisp = &priv->linedisp;
  30. struct linedisp_map *map = linedisp->map;
  31. DECLARE_BITMAP(values, 8) = { };
  32. bitmap_set_value8(values, map_to_seg7(&map->map.seg7, linedisp->buf[0]), 0);
  33. gpiod_multi_set_value_cansleep(priv->segment_gpios, values);
  34. }
  35. static int seg_led_linedisp_get_map_type(struct linedisp *linedisp)
  36. {
  37. struct seg_led_priv *priv = container_of(linedisp, struct seg_led_priv, linedisp);
  38. INIT_DELAYED_WORK(&priv->work, seg_led_update);
  39. return LINEDISP_MAP_SEG7;
  40. }
  41. static void seg_led_linedisp_update(struct linedisp *linedisp)
  42. {
  43. struct seg_led_priv *priv = container_of(linedisp, struct seg_led_priv, linedisp);
  44. schedule_delayed_work(&priv->work, 0);
  45. }
  46. static const struct linedisp_ops seg_led_linedisp_ops = {
  47. .get_map_type = seg_led_linedisp_get_map_type,
  48. .update = seg_led_linedisp_update,
  49. };
  50. static int seg_led_probe(struct platform_device *pdev)
  51. {
  52. struct seg_led_priv *priv;
  53. struct device *dev = &pdev->dev;
  54. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  55. if (!priv)
  56. return -ENOMEM;
  57. platform_set_drvdata(pdev, priv);
  58. priv->segment_gpios = devm_gpiod_get_array(dev, "segment", GPIOD_OUT_LOW);
  59. if (IS_ERR(priv->segment_gpios))
  60. return PTR_ERR(priv->segment_gpios);
  61. if (priv->segment_gpios->ndescs < 7 || priv->segment_gpios->ndescs > 8)
  62. return -EINVAL;
  63. return linedisp_register(&priv->linedisp, dev, 1, &seg_led_linedisp_ops);
  64. }
  65. static void seg_led_remove(struct platform_device *pdev)
  66. {
  67. struct seg_led_priv *priv = platform_get_drvdata(pdev);
  68. cancel_delayed_work_sync(&priv->work);
  69. linedisp_unregister(&priv->linedisp);
  70. }
  71. static const struct of_device_id seg_led_of_match[] = {
  72. { .compatible = "gpio-7-segment"},
  73. {}
  74. };
  75. MODULE_DEVICE_TABLE(of, seg_led_of_match);
  76. static struct platform_driver seg_led_driver = {
  77. .probe = seg_led_probe,
  78. .remove = seg_led_remove,
  79. .driver = {
  80. .name = "seg-led-gpio",
  81. .of_match_table = seg_led_of_match,
  82. },
  83. };
  84. module_platform_driver(seg_led_driver);
  85. MODULE_AUTHOR("Chris Packham <chris.packham@alliedtelesis.co.nz>");
  86. MODULE_DESCRIPTION("7 segment LED driver");
  87. MODULE_LICENSE("GPL");
  88. MODULE_IMPORT_NS("LINEDISP");