Thomas Gleixner | af873fc | 2019-05-28 09:57:21 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2007-2010 ST-Ericsson |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 4 | * Register access functions for the ABX500 Mixed Signal IC family. |
| 5 | * Author: Mattias Wallin <mattias.wallin@stericsson.com> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/list.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/err.h> |
Paul Gortmaker | fdae7ba | 2016-10-29 21:24:40 -0400 | [diff] [blame] | 11 | #include <linux/init.h> |
| 12 | #include <linux/export.h> |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 13 | #include <linux/mfd/abx500.h> |
| 14 | |
| 15 | static LIST_HEAD(abx500_list); |
| 16 | |
| 17 | struct abx500_device_entry { |
| 18 | struct list_head list; |
| 19 | struct abx500_ops ops; |
| 20 | struct device *dev; |
| 21 | }; |
| 22 | |
| 23 | static void lookup_ops(struct device *dev, struct abx500_ops **ops) |
| 24 | { |
| 25 | struct abx500_device_entry *dev_entry; |
| 26 | |
| 27 | *ops = NULL; |
| 28 | list_for_each_entry(dev_entry, &abx500_list, list) { |
| 29 | if (dev_entry->dev == dev) { |
| 30 | *ops = &dev_entry->ops; |
| 31 | return; |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | int abx500_register_ops(struct device *dev, struct abx500_ops *ops) |
| 37 | { |
| 38 | struct abx500_device_entry *dev_entry; |
| 39 | |
Markus Elfring | 5053e3a | 2018-03-08 12:46:47 +0100 | [diff] [blame] | 40 | dev_entry = devm_kzalloc(dev, sizeof(*dev_entry), GFP_KERNEL); |
Markus Elfring | a5e20bf | 2018-03-08 11:44:33 +0100 | [diff] [blame] | 41 | if (!dev_entry) |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 42 | return -ENOMEM; |
Markus Elfring | a5e20bf | 2018-03-08 11:44:33 +0100 | [diff] [blame] | 43 | |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 44 | dev_entry->dev = dev; |
Markus Elfring | 5053e3a | 2018-03-08 12:46:47 +0100 | [diff] [blame] | 45 | memcpy(&dev_entry->ops, ops, sizeof(*ops)); |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 46 | |
| 47 | list_add_tail(&dev_entry->list, &abx500_list); |
| 48 | return 0; |
| 49 | } |
| 50 | EXPORT_SYMBOL(abx500_register_ops); |
| 51 | |
| 52 | void abx500_remove_ops(struct device *dev) |
| 53 | { |
| 54 | struct abx500_device_entry *dev_entry, *tmp; |
| 55 | |
| 56 | list_for_each_entry_safe(dev_entry, tmp, &abx500_list, list) |
Lee Jones | c211b6b | 2013-05-23 16:25:07 +0100 | [diff] [blame] | 57 | if (dev_entry->dev == dev) |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 58 | list_del(&dev_entry->list); |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 59 | } |
| 60 | EXPORT_SYMBOL(abx500_remove_ops); |
| 61 | |
| 62 | int abx500_set_register_interruptible(struct device *dev, u8 bank, u8 reg, |
| 63 | u8 value) |
| 64 | { |
| 65 | struct abx500_ops *ops; |
| 66 | |
| 67 | lookup_ops(dev->parent, &ops); |
Markus Elfring | 6271299 | 2018-03-08 12:50:12 +0100 | [diff] [blame] | 68 | if (ops && ops->set_register) |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 69 | return ops->set_register(dev, bank, reg, value); |
| 70 | else |
| 71 | return -ENOTSUPP; |
| 72 | } |
| 73 | EXPORT_SYMBOL(abx500_set_register_interruptible); |
| 74 | |
| 75 | int abx500_get_register_interruptible(struct device *dev, u8 bank, u8 reg, |
| 76 | u8 *value) |
| 77 | { |
| 78 | struct abx500_ops *ops; |
| 79 | |
| 80 | lookup_ops(dev->parent, &ops); |
Markus Elfring | 6271299 | 2018-03-08 12:50:12 +0100 | [diff] [blame] | 81 | if (ops && ops->get_register) |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 82 | return ops->get_register(dev, bank, reg, value); |
| 83 | else |
| 84 | return -ENOTSUPP; |
| 85 | } |
| 86 | EXPORT_SYMBOL(abx500_get_register_interruptible); |
| 87 | |
| 88 | int abx500_get_register_page_interruptible(struct device *dev, u8 bank, |
| 89 | u8 first_reg, u8 *regvals, u8 numregs) |
| 90 | { |
| 91 | struct abx500_ops *ops; |
| 92 | |
| 93 | lookup_ops(dev->parent, &ops); |
Markus Elfring | 6271299 | 2018-03-08 12:50:12 +0100 | [diff] [blame] | 94 | if (ops && ops->get_register_page) |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 95 | return ops->get_register_page(dev, bank, |
| 96 | first_reg, regvals, numregs); |
| 97 | else |
| 98 | return -ENOTSUPP; |
| 99 | } |
| 100 | EXPORT_SYMBOL(abx500_get_register_page_interruptible); |
| 101 | |
| 102 | int abx500_mask_and_set_register_interruptible(struct device *dev, u8 bank, |
| 103 | u8 reg, u8 bitmask, u8 bitvalues) |
| 104 | { |
| 105 | struct abx500_ops *ops; |
| 106 | |
| 107 | lookup_ops(dev->parent, &ops); |
Markus Elfring | 6271299 | 2018-03-08 12:50:12 +0100 | [diff] [blame] | 108 | if (ops && ops->mask_and_set_register) |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 109 | return ops->mask_and_set_register(dev, bank, |
| 110 | reg, bitmask, bitvalues); |
| 111 | else |
| 112 | return -ENOTSUPP; |
| 113 | } |
| 114 | EXPORT_SYMBOL(abx500_mask_and_set_register_interruptible); |
| 115 | |
| 116 | int abx500_get_chip_id(struct device *dev) |
| 117 | { |
| 118 | struct abx500_ops *ops; |
| 119 | |
| 120 | lookup_ops(dev->parent, &ops); |
Markus Elfring | 6271299 | 2018-03-08 12:50:12 +0100 | [diff] [blame] | 121 | if (ops && ops->get_chip_id) |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 122 | return ops->get_chip_id(dev); |
| 123 | else |
| 124 | return -ENOTSUPP; |
| 125 | } |
| 126 | EXPORT_SYMBOL(abx500_get_chip_id); |
| 127 | |
| 128 | int abx500_event_registers_startup_state_get(struct device *dev, u8 *event) |
| 129 | { |
| 130 | struct abx500_ops *ops; |
| 131 | |
| 132 | lookup_ops(dev->parent, &ops); |
Markus Elfring | 6271299 | 2018-03-08 12:50:12 +0100 | [diff] [blame] | 133 | if (ops && ops->event_registers_startup_state_get) |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 134 | return ops->event_registers_startup_state_get(dev, event); |
| 135 | else |
| 136 | return -ENOTSUPP; |
| 137 | } |
| 138 | EXPORT_SYMBOL(abx500_event_registers_startup_state_get); |
| 139 | |
| 140 | int abx500_startup_irq_enabled(struct device *dev, unsigned int irq) |
| 141 | { |
| 142 | struct abx500_ops *ops; |
| 143 | |
| 144 | lookup_ops(dev->parent, &ops); |
Markus Elfring | 6271299 | 2018-03-08 12:50:12 +0100 | [diff] [blame] | 145 | if (ops && ops->startup_irq_enabled) |
Mattias Wallin | fa66125 | 2010-05-01 18:26:20 +0200 | [diff] [blame] | 146 | return ops->startup_irq_enabled(dev, irq); |
| 147 | else |
| 148 | return -ENOTSUPP; |
| 149 | } |
| 150 | EXPORT_SYMBOL(abx500_startup_irq_enabled); |