seg-led-gpio.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_set_array_value_cansleep(priv->segment_gpios->ndescs, priv->segment_gpios->desc,
  34. priv->segment_gpios->info, values);
  35. }
  36. static int seg_led_linedisp_get_map_type(struct linedisp *linedisp)
  37. {
  38. struct seg_led_priv *priv = container_of(linedisp, struct seg_led_priv, linedisp);
  39. INIT_DELAYED_WORK(&priv->work, seg_led_update);
  40. return LINEDISP_MAP_SEG7;
  41. }
  42. static void seg_led_linedisp_update(struct linedisp *linedisp)
  43. {
  44. struct seg_led_priv *priv = container_of(linedisp, struct seg_led_priv, linedisp);
  45. schedule_delayed_work(&priv->work, 0);
  46. }
  47. static const struct linedisp_ops seg_led_linedisp_ops = {
  48. .get_map_type = seg_led_linedisp_get_map_type,
  49. .update = seg_led_linedisp_update,
  50. };
  51. static int seg_led_probe(struct platform_device *pdev)
  52. {
  53. struct seg_led_priv *priv;
  54. struct device *dev = &pdev->dev;
  55. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  56. if (!priv)
  57. return -ENOMEM;
  58. platform_set_drvdata(pdev, priv);
  59. priv->segment_gpios = devm_gpiod_get_array(dev, "segment", GPIOD_OUT_LOW);
  60. if (IS_ERR(priv->segment_gpios))
  61. return PTR_ERR(priv->segment_gpios);
  62. if (priv->segment_gpios->ndescs < 7 || priv->segment_gpios->ndescs > 8)
  63. return -EINVAL;
  64. return linedisp_register(&priv->linedisp, dev, 1, &seg_led_linedisp_ops);
  65. }
  66. static void seg_led_remove(struct platform_device *pdev)
  67. {
  68. struct seg_led_priv *priv = platform_get_drvdata(pdev);
  69. cancel_delayed_work_sync(&priv->work);
  70. linedisp_unregister(&priv->linedisp);
  71. }
  72. static const struct of_device_id seg_led_of_match[] = {
  73. { .compatible = "gpio-7-segment"},
  74. {}
  75. };
  76. MODULE_DEVICE_TABLE(of, seg_led_of_match);
  77. static struct platform_driver seg_led_driver = {
  78. .probe = seg_led_probe,
  79. .remove_new = seg_led_remove,
  80. .driver = {
  81. .name = "seg-led-gpio",
  82. .of_match_table = seg_led_of_match,
  83. },
  84. };
  85. module_platform_driver(seg_led_driver);
  86. MODULE_AUTHOR("Chris Packham <chris.packham@alliedtelesis.co.nz>");
  87. MODULE_DESCRIPTION("7 segment LED driver");
  88. MODULE_LICENSE("GPL");
  89. MODULE_IMPORT_NS(LINEDISP);