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 11 #include <linux/bitmap.h> 12 #include <linux/container_of.h> 13 #include <linux/errno.h> 14 #include <linux/gpio/consumer.h> 15 #include <linux/map_to_7segment.h> 16 #include <linux/mod_devicetable.h> 17 #include <linux/module.h> 18 #include <linux/platform_device.h> 19 #include <linux/types.h> 20 #include <linux/workqueue.h> 21 22 #include "line-display.h" 23 24 struct seg_led_priv { 25 struct linedisp linedisp; 26 struct delayed_work work; 27 struct gpio_descs *segment_gpios; 28 }; 29 30 static void seg_led_update(struct work_struct *work) 31 { 32 struct seg_led_priv *priv = container_of(work, struct seg_led_priv, work.work); 33 struct linedisp *linedisp = &priv->linedisp; 34 struct linedisp_map *map = linedisp->map; 35 DECLARE_BITMAP(values, 8) = { }; 36 37 bitmap_set_value8(values, map_to_seg7(&map->map.seg7, linedisp->buf[0]), 0); 38 39 gpiod_set_array_value_cansleep(priv->segment_gpios->ndescs, priv->segment_gpios->desc, 40 priv->segment_gpios->info, values); 41 } 42 43 static int seg_led_linedisp_get_map_type(struct linedisp *linedisp) 44 { 45 struct seg_led_priv *priv = container_of(linedisp, struct seg_led_priv, linedisp); 46 47 INIT_DELAYED_WORK(&priv->work, seg_led_update); 48 return LINEDISP_MAP_SEG7; 49 } 50 51 static void seg_led_linedisp_update(struct linedisp *linedisp) 52 { 53 struct seg_led_priv *priv = container_of(linedisp, struct seg_led_priv, linedisp); 54 55 schedule_delayed_work(&priv->work, 0); 56 } 57 58 static const struct linedisp_ops seg_led_linedisp_ops = { 59 .get_map_type = seg_led_linedisp_get_map_type, 60 .update = seg_led_linedisp_update, 61 }; 62 63 static int seg_led_probe(struct platform_device *pdev) 64 { 65 struct seg_led_priv *priv; 66 struct device *dev = &pdev->dev; 67 68 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 69 if (!priv) 70 return -ENOMEM; 71 72 platform_set_drvdata(pdev, priv); 73 74 priv->segment_gpios = devm_gpiod_get_array(dev, "segment", GPIOD_OUT_LOW); 75 if (IS_ERR(priv->segment_gpios)) 76 return PTR_ERR(priv->segment_gpios); 77 78 if (priv->segment_gpios->ndescs < 7 || priv->segment_gpios->ndescs > 8) 79 return -EINVAL; 80 81 return linedisp_register(&priv->linedisp, dev, 1, &seg_led_linedisp_ops); 82 } 83 84 static void seg_led_remove(struct platform_device *pdev) 85 { 86 struct seg_led_priv *priv = platform_get_drvdata(pdev); 87 88 cancel_delayed_work_sync(&priv->work); 89 linedisp_unregister(&priv->linedisp); 90 } 91 92 static const struct of_device_id seg_led_of_match[] = { 93 { .compatible = "gpio-7-segment"}, 94 {} 95 }; 96 MODULE_DEVICE_TABLE(of, seg_led_of_match); 97 98 static struct platform_driver seg_led_driver = { 99 .probe = seg_led_probe, 100 .remove_new = seg_led_remove, 101 .driver = { 102 .name = "seg-led-gpio", 103 .of_match_table = seg_led_of_match, 104 }, 105 }; 106 module_platform_driver(seg_led_driver); 107 108 MODULE_AUTHOR("Chris Packham <chris.packham@alliedtelesis.co.nz>"); 109 MODULE_DESCRIPTION("7 segment LED driver"); 110 MODULE_LICENSE("GPL"); 111 MODULE_IMPORT_NS(LINEDISP); 112