1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * MAX6958/6959 7-segment LED display controller 4 * Datasheet: 5 * https://www.analog.com/media/en/technical-documentation/data-sheets/MAX6958-MAX6959.pdf 6 * 7 * Copyright (c) 2024, Intel Corporation. 8 * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com> 9 */ 10 #include <linux/array_size.h> 11 #include <linux/bitrev.h> 12 #include <linux/bits.h> 13 #include <linux/container_of.h> 14 #include <linux/device/devres.h> 15 #include <linux/err.h> 16 #include <linux/i2c.h> 17 #include <linux/module.h> 18 #include <linux/pm.h> 19 #include <linux/regmap.h> 20 #include <linux/types.h> 21 #include <linux/workqueue.h> 22 23 #include <linux/map_to_7segment.h> 24 25 #include "line-display.h" 26 27 /* Registers */ 28 #define REG_DECODE_MODE 0x01 29 #define REG_INTENSITY 0x02 30 #define REG_SCAN_LIMIT 0x03 31 #define REG_CONFIGURATION 0x04 32 #define REG_CONFIGURATION_S_BIT BIT(0) 33 34 #define REG_DIGIT(x) (0x20 + (x)) 35 #define REG_DIGIT0 0x20 36 #define REG_DIGIT1 0x21 37 #define REG_DIGIT2 0x22 38 #define REG_DIGIT3 0x23 39 40 #define REG_SEGMENTS 0x24 41 #define REG_MAX REG_SEGMENTS 42 43 struct max6959_priv { 44 struct linedisp linedisp; 45 struct delayed_work work; 46 struct regmap *regmap; 47 }; 48 49 static void max6959_disp_update(struct work_struct *work) 50 { 51 struct max6959_priv *priv = container_of(work, struct max6959_priv, work.work); 52 struct linedisp *linedisp = &priv->linedisp; 53 struct linedisp_map *map = linedisp->map; 54 char *s = linedisp->buf; 55 u8 buf[4]; 56 57 /* Map segments according to datasheet */ 58 buf[0] = bitrev8(map_to_seg7(&map->map.seg7, *s++)) >> 1; 59 buf[1] = bitrev8(map_to_seg7(&map->map.seg7, *s++)) >> 1; 60 buf[2] = bitrev8(map_to_seg7(&map->map.seg7, *s++)) >> 1; 61 buf[3] = bitrev8(map_to_seg7(&map->map.seg7, *s++)) >> 1; 62 63 regmap_bulk_write(priv->regmap, REG_DIGIT(0), buf, ARRAY_SIZE(buf)); 64 } 65 66 static int max6959_linedisp_get_map_type(struct linedisp *linedisp) 67 { 68 struct max6959_priv *priv = container_of(linedisp, struct max6959_priv, linedisp); 69 70 INIT_DELAYED_WORK(&priv->work, max6959_disp_update); 71 return LINEDISP_MAP_SEG7; 72 } 73 74 static void max6959_linedisp_update(struct linedisp *linedisp) 75 { 76 struct max6959_priv *priv = container_of(linedisp, struct max6959_priv, linedisp); 77 78 schedule_delayed_work(&priv->work, 0); 79 } 80 81 static const struct linedisp_ops max6959_linedisp_ops = { 82 .get_map_type = max6959_linedisp_get_map_type, 83 .update = max6959_linedisp_update, 84 }; 85 86 static int max6959_enable(struct max6959_priv *priv, bool enable) 87 { 88 return regmap_assign_bits(priv->regmap, REG_CONFIGURATION, REG_CONFIGURATION_S_BIT, enable); 89 } 90 91 static void max6959_power_off(void *priv) 92 { 93 max6959_enable(priv, false); 94 } 95 96 static int max6959_power_on(struct max6959_priv *priv) 97 { 98 struct device *dev = regmap_get_device(priv->regmap); 99 int ret; 100 101 ret = max6959_enable(priv, true); 102 if (ret) 103 return ret; 104 105 return devm_add_action_or_reset(dev, max6959_power_off, priv); 106 } 107 108 static const struct regmap_config max6959_regmap_config = { 109 .reg_bits = 8, 110 .val_bits = 8, 111 112 .max_register = REG_MAX, 113 .cache_type = REGCACHE_MAPLE, 114 }; 115 116 static int max6959_i2c_probe(struct i2c_client *client) 117 { 118 struct device *dev = &client->dev; 119 struct max6959_priv *priv; 120 int ret; 121 122 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 123 if (!priv) 124 return -ENOMEM; 125 126 priv->regmap = devm_regmap_init_i2c(client, &max6959_regmap_config); 127 if (IS_ERR(priv->regmap)) 128 return PTR_ERR(priv->regmap); 129 130 ret = max6959_power_on(priv); 131 if (ret) 132 return ret; 133 134 ret = linedisp_register(&priv->linedisp, dev, 4, &max6959_linedisp_ops); 135 if (ret) 136 return ret; 137 138 i2c_set_clientdata(client, priv); 139 140 return 0; 141 } 142 143 static void max6959_i2c_remove(struct i2c_client *client) 144 { 145 struct max6959_priv *priv = i2c_get_clientdata(client); 146 147 cancel_delayed_work_sync(&priv->work); 148 linedisp_unregister(&priv->linedisp); 149 } 150 151 static int max6959_suspend(struct device *dev) 152 { 153 return max6959_enable(dev_get_drvdata(dev), false); 154 } 155 156 static int max6959_resume(struct device *dev) 157 { 158 return max6959_enable(dev_get_drvdata(dev), true); 159 } 160 161 static DEFINE_SIMPLE_DEV_PM_OPS(max6959_pm_ops, max6959_suspend, max6959_resume); 162 163 static const struct i2c_device_id max6959_i2c_id[] = { 164 { "max6959" }, 165 { } 166 }; 167 MODULE_DEVICE_TABLE(i2c, max6959_i2c_id); 168 169 static const struct of_device_id max6959_of_table[] = { 170 { .compatible = "maxim,max6959" }, 171 { } 172 }; 173 MODULE_DEVICE_TABLE(of, max6959_of_table); 174 175 static struct i2c_driver max6959_i2c_driver = { 176 .driver = { 177 .name = "max6959", 178 .pm = pm_sleep_ptr(&max6959_pm_ops), 179 .of_match_table = max6959_of_table, 180 }, 181 .probe = max6959_i2c_probe, 182 .remove = max6959_i2c_remove, 183 .id_table = max6959_i2c_id, 184 }; 185 module_i2c_driver(max6959_i2c_driver); 186 187 MODULE_DESCRIPTION("MAX6958/6959 7-segment LED controller"); 188 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>"); 189 MODULE_LICENSE("GPL"); 190 MODULE_IMPORT_NS("LINEDISP"); 191