Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1 | /* |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 2 | * TI Bandgap temperature sensor driver |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2011-2012 Texas Instruments Incorporated - http://www.ti.com/ |
| 5 | * Author: J Keerthy <j-keerthy@ti.com> |
| 6 | * Author: Moiz Sonasath <m-sonasath@ti.com> |
| 7 | * Couple of fixes, DT and MFD adaptation: |
| 8 | * Eduardo Valentin <eduardo.valentin@ti.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License |
| 12 | * version 2 as published by the Free Software Foundation. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but |
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 22 | * 02110-1301 USA |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/export.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/interrupt.h> |
| 31 | #include <linux/clk.h> |
| 32 | #include <linux/gpio.h> |
| 33 | #include <linux/platform_device.h> |
| 34 | #include <linux/err.h> |
| 35 | #include <linux/types.h> |
Eduardo Valentin | ebf0bd5 | 2013-03-15 09:00:35 -0400 | [diff] [blame] | 36 | #include <linux/spinlock.h> |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 37 | #include <linux/reboot.h> |
| 38 | #include <linux/of_device.h> |
| 39 | #include <linux/of_platform.h> |
| 40 | #include <linux/of_irq.h> |
Eduardo Valentin | 57d1617 | 2013-06-07 11:11:53 -0400 | [diff] [blame] | 41 | #include <linux/of_gpio.h> |
Eduardo Valentin | 2aeeb8a | 2012-11-13 14:10:00 -0400 | [diff] [blame] | 42 | #include <linux/io.h> |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 43 | |
Eduardo Valentin | 7372add | 2013-03-19 10:54:19 -0400 | [diff] [blame] | 44 | #include "ti-bandgap.h" |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 45 | |
Eduardo Valentin | 8abbe71 | 2013-03-15 09:00:05 -0400 | [diff] [blame] | 46 | /*** Helper functions to access registers and their bitfields ***/ |
| 47 | |
Eduardo Valentin | 9c468aa | 2013-03-15 08:59:56 -0400 | [diff] [blame] | 48 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 49 | * ti_bandgap_readl() - simple read helper function |
| 50 | * @bgp: pointer to ti_bandgap structure |
Eduardo Valentin | 9c468aa | 2013-03-15 08:59:56 -0400 | [diff] [blame] | 51 | * @reg: desired register (offset) to be read |
| 52 | * |
| 53 | * Helper function to read bandgap registers. It uses the io remapped area. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 54 | * Return: the register value. |
Eduardo Valentin | 9c468aa | 2013-03-15 08:59:56 -0400 | [diff] [blame] | 55 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 56 | static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 57 | { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 58 | return readl(bgp->base + reg); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 59 | } |
| 60 | |
Eduardo Valentin | 9c468aa | 2013-03-15 08:59:56 -0400 | [diff] [blame] | 61 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 62 | * ti_bandgap_writel() - simple write helper function |
| 63 | * @bgp: pointer to ti_bandgap structure |
Eduardo Valentin | 9c468aa | 2013-03-15 08:59:56 -0400 | [diff] [blame] | 64 | * @val: desired register value to be written |
| 65 | * @reg: desired register (offset) to be written |
| 66 | * |
| 67 | * Helper function to write bandgap registers. It uses the io remapped area. |
| 68 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 69 | static void ti_bandgap_writel(struct ti_bandgap *bgp, u32 val, u32 reg) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 70 | { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 71 | writel(val, bgp->base + reg); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 72 | } |
| 73 | |
Eduardo Valentin | 9c468aa | 2013-03-15 08:59:56 -0400 | [diff] [blame] | 74 | /** |
| 75 | * DOC: macro to update bits. |
| 76 | * |
| 77 | * RMW_BITS() - used to read, modify and update bandgap bitfields. |
| 78 | * The value passed will be shifted. |
| 79 | */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 80 | #define RMW_BITS(bgp, id, reg, mask, val) \ |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 81 | do { \ |
| 82 | struct temp_sensor_registers *t; \ |
| 83 | u32 r; \ |
| 84 | \ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 85 | t = bgp->conf->sensors[(id)].registers; \ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 86 | r = ti_bandgap_readl(bgp, t->reg); \ |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 87 | r &= ~t->mask; \ |
| 88 | r |= (val) << __ffs(t->mask); \ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 89 | ti_bandgap_writel(bgp, r, t->reg); \ |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 90 | } while (0) |
| 91 | |
Eduardo Valentin | 6ab5240 | 2013-03-15 09:00:06 -0400 | [diff] [blame] | 92 | /*** Basic helper functions ***/ |
| 93 | |
Eduardo Valentin | 7a556e6 | 2013-03-15 08:59:58 -0400 | [diff] [blame] | 94 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 95 | * ti_bandgap_power() - controls the power state of a bandgap device |
| 96 | * @bgp: pointer to ti_bandgap structure |
Eduardo Valentin | 7a556e6 | 2013-03-15 08:59:58 -0400 | [diff] [blame] | 97 | * @on: desired power state (1 - on, 0 - off) |
| 98 | * |
| 99 | * Used to power on/off a bandgap device instance. Only used on those |
| 100 | * that features tempsoff bit. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 101 | * |
| 102 | * Return: 0 on success, -ENOTSUPP if tempsoff is not supported. |
Eduardo Valentin | 7a556e6 | 2013-03-15 08:59:58 -0400 | [diff] [blame] | 103 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 104 | static int ti_bandgap_power(struct ti_bandgap *bgp, bool on) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 105 | { |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 106 | int i; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 107 | |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 108 | if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH)) |
| 109 | return -ENOTSUPP; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 110 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 111 | for (i = 0; i < bgp->conf->sensor_count; i++) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 112 | /* active on 0 */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 113 | RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on); |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 114 | return 0; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 115 | } |
| 116 | |
Eduardo Valentin | 4a6554e | 2013-03-15 08:59:59 -0400 | [diff] [blame] | 117 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 118 | * ti_bandgap_read_temp() - helper function to read sensor temperature |
| 119 | * @bgp: pointer to ti_bandgap structure |
Eduardo Valentin | 4a6554e | 2013-03-15 08:59:59 -0400 | [diff] [blame] | 120 | * @id: bandgap sensor id |
| 121 | * |
| 122 | * Function to concentrate the steps to read sensor temperature register. |
| 123 | * This function is desired because, depending on bandgap device version, |
| 124 | * it might be needed to freeze the bandgap state machine, before fetching |
| 125 | * the register value. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 126 | * |
| 127 | * Return: temperature in ADC values. |
Eduardo Valentin | 4a6554e | 2013-03-15 08:59:59 -0400 | [diff] [blame] | 128 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 129 | static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id) |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 130 | { |
| 131 | struct temp_sensor_registers *tsr; |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 132 | u32 temp, reg; |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 133 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 134 | tsr = bgp->conf->sensors[id].registers; |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 135 | reg = tsr->temp_sensor_ctrl; |
| 136 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 137 | if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 138 | RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1); |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 139 | /* |
| 140 | * In case we cannot read from cur_dtemp / dtemp_0, |
| 141 | * then we read from the last valid temp read |
| 142 | */ |
| 143 | reg = tsr->ctrl_dtemp_1; |
| 144 | } |
| 145 | |
| 146 | /* read temperature */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 147 | temp = ti_bandgap_readl(bgp, reg); |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 148 | temp &= tsr->bgap_dtemp_mask; |
| 149 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 150 | if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 151 | RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0); |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 152 | |
| 153 | return temp; |
| 154 | } |
| 155 | |
Eduardo Valentin | fb65b88 | 2013-03-15 09:00:07 -0400 | [diff] [blame] | 156 | /*** IRQ handlers ***/ |
| 157 | |
Eduardo Valentin | ee07d55 | 2013-03-15 09:00:01 -0400 | [diff] [blame] | 158 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 159 | * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs |
Eduardo Valentin | ee07d55 | 2013-03-15 09:00:01 -0400 | [diff] [blame] | 160 | * @irq: IRQ number |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 161 | * @data: private data (struct ti_bandgap *) |
Eduardo Valentin | ee07d55 | 2013-03-15 09:00:01 -0400 | [diff] [blame] | 162 | * |
| 163 | * This is the Talert handler. Use it only if bandgap device features |
| 164 | * HAS(TALERT). This handler goes over all sensors and checks their |
| 165 | * conditions and acts accordingly. In case there are events pending, |
| 166 | * it will reset the event mask to wait for the opposite event (next event). |
| 167 | * Every time there is a new event, it will be reported to thermal layer. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 168 | * |
| 169 | * Return: IRQ_HANDLED |
Eduardo Valentin | ee07d55 | 2013-03-15 09:00:01 -0400 | [diff] [blame] | 170 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 171 | static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 172 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 173 | struct ti_bandgap *bgp = data; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 174 | struct temp_sensor_registers *tsr; |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 175 | u32 t_hot = 0, t_cold = 0, ctrl; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 176 | int i; |
| 177 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 178 | spin_lock(&bgp->lock); |
| 179 | for (i = 0; i < bgp->conf->sensor_count; i++) { |
| 180 | tsr = bgp->conf->sensors[i].registers; |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 181 | ctrl = ti_bandgap_readl(bgp, tsr->bgap_status); |
Eduardo Valentin | e555c95 | 2013-03-15 09:00:04 -0400 | [diff] [blame] | 182 | |
| 183 | /* Read the status of t_hot */ |
| 184 | t_hot = ctrl & tsr->status_hot_mask; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 185 | |
| 186 | /* Read the status of t_cold */ |
Eduardo Valentin | e555c95 | 2013-03-15 09:00:04 -0400 | [diff] [blame] | 187 | t_cold = ctrl & tsr->status_cold_mask; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 188 | |
| 189 | if (!t_cold && !t_hot) |
| 190 | continue; |
| 191 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 192 | ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 193 | /* |
| 194 | * One TALERT interrupt: Two sources |
| 195 | * If the interrupt is due to t_hot then mask t_hot and |
| 196 | * and unmask t_cold else mask t_cold and unmask t_hot |
| 197 | */ |
| 198 | if (t_hot) { |
| 199 | ctrl &= ~tsr->mask_hot_mask; |
| 200 | ctrl |= tsr->mask_cold_mask; |
| 201 | } else if (t_cold) { |
| 202 | ctrl &= ~tsr->mask_cold_mask; |
| 203 | ctrl |= tsr->mask_hot_mask; |
| 204 | } |
| 205 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 206 | ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 207 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 208 | dev_dbg(bgp->dev, |
Eduardo Valentin | 71e303f | 2012-11-13 14:10:03 -0400 | [diff] [blame] | 209 | "%s: IRQ from %s sensor: hotevent %d coldevent %d\n", |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 210 | __func__, bgp->conf->sensors[i].domain, |
Eduardo Valentin | 71e303f | 2012-11-13 14:10:03 -0400 | [diff] [blame] | 211 | t_hot, t_cold); |
| 212 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 213 | /* report temperature to whom may concern */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 214 | if (bgp->conf->report_temperature) |
| 215 | bgp->conf->report_temperature(bgp, i); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 216 | } |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 217 | spin_unlock(&bgp->lock); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 218 | |
| 219 | return IRQ_HANDLED; |
| 220 | } |
| 221 | |
Eduardo Valentin | 79857cd2 | 2013-03-15 09:00:02 -0400 | [diff] [blame] | 222 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 223 | * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal |
Eduardo Valentin | 79857cd2 | 2013-03-15 09:00:02 -0400 | [diff] [blame] | 224 | * @irq: IRQ number |
| 225 | * @data: private data (unused) |
| 226 | * |
| 227 | * This is the Tshut handler. Use it only if bandgap device features |
| 228 | * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown |
| 229 | * the system. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 230 | * |
| 231 | * Return: IRQ_HANDLED |
Eduardo Valentin | 79857cd2 | 2013-03-15 09:00:02 -0400 | [diff] [blame] | 232 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 233 | static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 234 | { |
Ruslan Ruslichenko | b3bf0e9 | 2013-02-26 18:53:24 -0400 | [diff] [blame] | 235 | pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n", |
| 236 | __func__); |
| 237 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 238 | orderly_poweroff(true); |
| 239 | |
| 240 | return IRQ_HANDLED; |
| 241 | } |
| 242 | |
Eduardo Valentin | 2f6af4b | 2013-03-15 09:00:08 -0400 | [diff] [blame] | 243 | /*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/ |
| 244 | |
Eduardo Valentin | 2577e93 | 2013-03-15 09:00:11 -0400 | [diff] [blame] | 245 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 246 | * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale |
| 247 | * @bgp: struct ti_bandgap pointer |
Eduardo Valentin | 2577e93 | 2013-03-15 09:00:11 -0400 | [diff] [blame] | 248 | * @adc_val: value in ADC representation |
| 249 | * @t: address where to write the resulting temperature in mCelsius |
| 250 | * |
| 251 | * Simple conversion from ADC representation to mCelsius. In case the ADC value |
| 252 | * is out of the ADC conv table range, it returns -ERANGE, 0 on success. |
| 253 | * The conversion table is indexed by the ADC values. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 254 | * |
| 255 | * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val |
| 256 | * argument is out of the ADC conv table range. |
Eduardo Valentin | 2577e93 | 2013-03-15 09:00:11 -0400 | [diff] [blame] | 257 | */ |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 258 | static |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 259 | int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 260 | { |
Eduardo Valentin | 9879b2c | 2013-03-19 10:54:23 -0400 | [diff] [blame] | 261 | const struct ti_bandgap_data *conf = bgp->conf; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 262 | |
| 263 | /* look up for temperature in the table and return the temperature */ |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 264 | if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val) |
| 265 | return -ERANGE; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 266 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 267 | *t = bgp->conf->conv_table[adc_val - conf->adc_start_val]; |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 268 | return 0; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 269 | } |
| 270 | |
Eduardo Valentin | e7f60b5 | 2013-03-15 09:00:15 -0400 | [diff] [blame] | 271 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 272 | * ti_bandgap_mcelsius_to_adc() - converts a mCelsius value to ADC scale |
| 273 | * @bgp: struct ti_bandgap pointer |
Eduardo Valentin | e7f60b5 | 2013-03-15 09:00:15 -0400 | [diff] [blame] | 274 | * @temp: value in mCelsius |
| 275 | * @adc: address where to write the resulting temperature in ADC representation |
| 276 | * |
| 277 | * Simple conversion from mCelsius to ADC values. In case the temp value |
| 278 | * is out of the ADC conv table range, it returns -ERANGE, 0 on success. |
| 279 | * The conversion table is indexed by the ADC values. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 280 | * |
| 281 | * Return: 0 if conversion was successful, else -ERANGE in case the @temp |
| 282 | * argument is out of the ADC conv table range. |
Eduardo Valentin | e7f60b5 | 2013-03-15 09:00:15 -0400 | [diff] [blame] | 283 | */ |
Eduardo Valentin | e16f072 | 2013-03-15 09:00:12 -0400 | [diff] [blame] | 284 | static |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 285 | int ti_bandgap_mcelsius_to_adc(struct ti_bandgap *bgp, long temp, int *adc) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 286 | { |
Eduardo Valentin | 9879b2c | 2013-03-19 10:54:23 -0400 | [diff] [blame] | 287 | const struct ti_bandgap_data *conf = bgp->conf; |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 288 | const int *conv_table = bgp->conf->conv_table; |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 289 | int high, low, mid; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 290 | |
| 291 | low = 0; |
Eduardo Valentin | 26a70ed | 2013-03-15 09:00:14 -0400 | [diff] [blame] | 292 | high = conf->adc_end_val - conf->adc_start_val; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 293 | mid = (high + low) / 2; |
| 294 | |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 295 | if (temp < conv_table[low] || temp > conv_table[high]) |
| 296 | return -ERANGE; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 297 | |
| 298 | while (low < high) { |
Eduardo Valentin | c8a8f84 | 2013-02-26 18:53:36 -0400 | [diff] [blame] | 299 | if (temp < conv_table[mid]) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 300 | high = mid - 1; |
| 301 | else |
| 302 | low = mid + 1; |
| 303 | mid = (low + high) / 2; |
| 304 | } |
| 305 | |
Eduardo Valentin | 26a70ed | 2013-03-15 09:00:14 -0400 | [diff] [blame] | 306 | *adc = conf->adc_start_val + low; |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 307 | return 0; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 308 | } |
| 309 | |
Eduardo Valentin | 8a1cefe | 2013-03-15 09:00:17 -0400 | [diff] [blame] | 310 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 311 | * ti_bandgap_add_hyst() - add hysteresis (in mCelsius) to an ADC value |
| 312 | * @bgp: struct ti_bandgap pointer |
Eduardo Valentin | 8a1cefe | 2013-03-15 09:00:17 -0400 | [diff] [blame] | 313 | * @adc_val: temperature value in ADC representation |
| 314 | * @hyst_val: hysteresis value in mCelsius |
| 315 | * @sum: address where to write the resulting temperature (in ADC scale) |
| 316 | * |
| 317 | * Adds an hysteresis value (in mCelsius) to a ADC temperature value. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 318 | * |
| 319 | * Return: 0 on success, -ERANGE otherwise. |
Eduardo Valentin | 8a1cefe | 2013-03-15 09:00:17 -0400 | [diff] [blame] | 320 | */ |
Eduardo Valentin | 0f0ed7d | 2013-03-15 09:00:16 -0400 | [diff] [blame] | 321 | static |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 322 | int ti_bandgap_add_hyst(struct ti_bandgap *bgp, int adc_val, int hyst_val, |
| 323 | u32 *sum) |
Eduardo Valentin | 0f0ed7d | 2013-03-15 09:00:16 -0400 | [diff] [blame] | 324 | { |
| 325 | int temp, ret; |
| 326 | |
| 327 | /* |
| 328 | * Need to add in the mcelsius domain, so we have a temperature |
| 329 | * the conv_table range |
| 330 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 331 | ret = ti_bandgap_adc_to_mcelsius(bgp, adc_val, &temp); |
Eduardo Valentin | 0f0ed7d | 2013-03-15 09:00:16 -0400 | [diff] [blame] | 332 | if (ret < 0) |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 333 | return ret; |
Eduardo Valentin | 0f0ed7d | 2013-03-15 09:00:16 -0400 | [diff] [blame] | 334 | |
| 335 | temp += hyst_val; |
| 336 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 337 | ret = ti_bandgap_mcelsius_to_adc(bgp, temp, sum); |
Eduardo Valentin | 0f0ed7d | 2013-03-15 09:00:16 -0400 | [diff] [blame] | 338 | return ret; |
| 339 | } |
| 340 | |
Eduardo Valentin | f8ccce2 | 2013-03-15 09:00:18 -0400 | [diff] [blame] | 341 | /*** Helper functions handling device Alert/Shutdown signals ***/ |
| 342 | |
Eduardo Valentin | f47f6d3 | 2013-03-15 09:00:20 -0400 | [diff] [blame] | 343 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 344 | * ti_bandgap_unmask_interrupts() - unmasks the events of thot & tcold |
| 345 | * @bgp: struct ti_bandgap pointer |
Eduardo Valentin | 61603af | 2013-03-19 10:54:25 -0400 | [diff] [blame] | 346 | * @id: bandgap sensor id |
Eduardo Valentin | f47f6d3 | 2013-03-15 09:00:20 -0400 | [diff] [blame] | 347 | * @t_hot: hot temperature value to trigger alert signal |
| 348 | * @t_cold: cold temperature value to trigger alert signal |
| 349 | * |
| 350 | * Checks the requested t_hot and t_cold values and configures the IRQ event |
| 351 | * masks accordingly. Call this function only if bandgap features HAS(TALERT). |
| 352 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 353 | static void ti_bandgap_unmask_interrupts(struct ti_bandgap *bgp, int id, |
| 354 | u32 t_hot, u32 t_cold) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 355 | { |
| 356 | struct temp_sensor_registers *tsr; |
| 357 | u32 temp, reg_val; |
| 358 | |
| 359 | /* Read the current on die temperature */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 360 | temp = ti_bandgap_read_temp(bgp, id); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 361 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 362 | tsr = bgp->conf->sensors[id].registers; |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 363 | reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl); |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 364 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 365 | if (temp < t_hot) |
| 366 | reg_val |= tsr->mask_hot_mask; |
| 367 | else |
| 368 | reg_val &= ~tsr->mask_hot_mask; |
| 369 | |
| 370 | if (t_cold < temp) |
| 371 | reg_val |= tsr->mask_cold_mask; |
| 372 | else |
| 373 | reg_val &= ~tsr->mask_cold_mask; |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 374 | ti_bandgap_writel(bgp, reg_val, tsr->bgap_mask_ctrl); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 375 | } |
| 376 | |
Eduardo Valentin | 38d99e8 | 2013-03-15 09:00:27 -0400 | [diff] [blame] | 377 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 378 | * ti_bandgap_update_alert_threshold() - sequence to update thresholds |
| 379 | * @bgp: struct ti_bandgap pointer |
Eduardo Valentin | 38d99e8 | 2013-03-15 09:00:27 -0400 | [diff] [blame] | 380 | * @id: bandgap sensor id |
| 381 | * @val: value (ADC) of a new threshold |
| 382 | * @hot: desired threshold to be updated. true if threshold hot, false if |
| 383 | * threshold cold |
| 384 | * |
| 385 | * It will program the required thresholds (hot and cold) for TALERT signal. |
| 386 | * This function can be used to update t_hot or t_cold, depending on @hot value. |
| 387 | * It checks the resulting t_hot and t_cold values, based on the new passed @val |
| 388 | * and configures the thresholds so that t_hot is always greater than t_cold. |
| 389 | * Call this function only if bandgap features HAS(TALERT). |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 390 | * |
| 391 | * Return: 0 if no error, else corresponding error |
Eduardo Valentin | 38d99e8 | 2013-03-15 09:00:27 -0400 | [diff] [blame] | 392 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 393 | static int ti_bandgap_update_alert_threshold(struct ti_bandgap *bgp, int id, |
| 394 | int val, bool hot) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 395 | { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 396 | struct temp_sensor_data *ts_data = bgp->conf->sensors[id].ts_data; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 397 | struct temp_sensor_registers *tsr; |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 398 | u32 thresh_val, reg_val, t_hot, t_cold; |
| 399 | int err = 0; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 400 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 401 | tsr = bgp->conf->sensors[id].registers; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 402 | |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 403 | /* obtain the current value */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 404 | thresh_val = ti_bandgap_readl(bgp, tsr->bgap_threshold); |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 405 | t_cold = (thresh_val & tsr->threshold_tcold_mask) >> |
| 406 | __ffs(tsr->threshold_tcold_mask); |
| 407 | t_hot = (thresh_val & tsr->threshold_thot_mask) >> |
| 408 | __ffs(tsr->threshold_thot_mask); |
| 409 | if (hot) |
| 410 | t_hot = val; |
| 411 | else |
| 412 | t_cold = val; |
| 413 | |
Eduardo Valentin | f5d43b7 | 2013-03-19 10:54:26 -0400 | [diff] [blame] | 414 | if (t_cold > t_hot) { |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 415 | if (hot) |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 416 | err = ti_bandgap_add_hyst(bgp, t_hot, |
| 417 | -ts_data->hyst_val, |
| 418 | &t_cold); |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 419 | else |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 420 | err = ti_bandgap_add_hyst(bgp, t_cold, |
| 421 | ts_data->hyst_val, |
| 422 | &t_hot); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 423 | } |
| 424 | |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 425 | /* write the new threshold values */ |
Eduardo Valentin | 0fb3c24 | 2013-03-19 10:54:27 -0400 | [diff] [blame] | 426 | reg_val = thresh_val & |
| 427 | ~(tsr->threshold_thot_mask | tsr->threshold_tcold_mask); |
| 428 | reg_val |= (t_hot << __ffs(tsr->threshold_thot_mask)) | |
| 429 | (t_cold << __ffs(tsr->threshold_tcold_mask)); |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 430 | ti_bandgap_writel(bgp, reg_val, tsr->bgap_threshold); |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 431 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 432 | if (err) { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 433 | dev_err(bgp->dev, "failed to reprogram thot threshold\n"); |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 434 | err = -EIO; |
| 435 | goto exit; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 436 | } |
| 437 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 438 | ti_bandgap_unmask_interrupts(bgp, id, t_hot, t_cold); |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 439 | exit: |
| 440 | return err; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 441 | } |
| 442 | |
Eduardo Valentin | e72b7bb | 2013-03-15 09:00:38 -0400 | [diff] [blame] | 443 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 444 | * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap |
| 445 | * @bgp: struct ti_bandgap pointer |
Eduardo Valentin | e72b7bb | 2013-03-15 09:00:38 -0400 | [diff] [blame] | 446 | * @id: bandgap sensor id |
| 447 | * |
| 448 | * Checks if the bandgap pointer is valid and if the sensor id is also |
| 449 | * applicable. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 450 | * |
| 451 | * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if |
| 452 | * @id cannot index @bgp sensors. |
Eduardo Valentin | e72b7bb | 2013-03-15 09:00:38 -0400 | [diff] [blame] | 453 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 454 | static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 455 | { |
Eduardo Valentin | 0c12b5a | 2013-05-29 15:07:43 +0000 | [diff] [blame] | 456 | if (!bgp || IS_ERR(bgp)) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 457 | pr_err("%s: invalid bandgap pointer\n", __func__); |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 458 | return -EINVAL; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 459 | } |
| 460 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 461 | if ((id < 0) || (id >= bgp->conf->sensor_count)) { |
| 462 | dev_err(bgp->dev, "%s: sensor id out of range (%d)\n", |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 463 | __func__, id); |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 464 | return -ERANGE; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 465 | } |
| 466 | |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 467 | return 0; |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 468 | } |
| 469 | |
Eduardo Valentin | 9efa93b | 2013-03-15 09:00:28 -0400 | [diff] [blame] | 470 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 471 | * _ti_bandgap_write_threshold() - helper to update TALERT t_cold or t_hot |
| 472 | * @bgp: struct ti_bandgap pointer |
Eduardo Valentin | 9efa93b | 2013-03-15 09:00:28 -0400 | [diff] [blame] | 473 | * @id: bandgap sensor id |
| 474 | * @val: value (mCelsius) of a new threshold |
| 475 | * @hot: desired threshold to be updated. true if threshold hot, false if |
| 476 | * threshold cold |
| 477 | * |
| 478 | * It will update the required thresholds (hot and cold) for TALERT signal. |
| 479 | * This function can be used to update t_hot or t_cold, depending on @hot value. |
| 480 | * Validates the mCelsius range and update the requested threshold. |
| 481 | * Call this function only if bandgap features HAS(TALERT). |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 482 | * |
| 483 | * Return: 0 if no error, else corresponding error value. |
Eduardo Valentin | 9efa93b | 2013-03-15 09:00:28 -0400 | [diff] [blame] | 484 | */ |
Eduardo Valentin | 2f8ec2a | 2013-03-19 10:54:22 -0400 | [diff] [blame] | 485 | static int _ti_bandgap_write_threshold(struct ti_bandgap *bgp, int id, int val, |
| 486 | bool hot) |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 487 | { |
| 488 | struct temp_sensor_data *ts_data; |
| 489 | struct temp_sensor_registers *tsr; |
| 490 | u32 adc_val; |
| 491 | int ret; |
| 492 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 493 | ret = ti_bandgap_validate(bgp, id); |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 494 | if (ret) |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 495 | return ret; |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 496 | |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 497 | if (!TI_BANDGAP_HAS(bgp, TALERT)) |
| 498 | return -ENOTSUPP; |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 499 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 500 | ts_data = bgp->conf->sensors[id].ts_data; |
| 501 | tsr = bgp->conf->sensors[id].registers; |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 502 | if (hot) { |
| 503 | if (val < ts_data->min_temp + ts_data->hyst_val) |
| 504 | ret = -EINVAL; |
| 505 | } else { |
| 506 | if (val > ts_data->max_temp + ts_data->hyst_val) |
| 507 | ret = -EINVAL; |
| 508 | } |
| 509 | |
| 510 | if (ret) |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 511 | return ret; |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 512 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 513 | ret = ti_bandgap_mcelsius_to_adc(bgp, val, &adc_val); |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 514 | if (ret < 0) |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 515 | return ret; |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 516 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 517 | spin_lock(&bgp->lock); |
Eduardo Valentin | d52361c | 2013-03-19 10:54:28 -0400 | [diff] [blame] | 518 | ret = ti_bandgap_update_alert_threshold(bgp, id, adc_val, hot); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 519 | spin_unlock(&bgp->lock); |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 520 | return ret; |
| 521 | } |
| 522 | |
Eduardo Valentin | 7a681a5 | 2013-03-15 09:00:29 -0400 | [diff] [blame] | 523 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 524 | * _ti_bandgap_read_threshold() - helper to read TALERT t_cold or t_hot |
| 525 | * @bgp: struct ti_bandgap pointer |
Eduardo Valentin | 7a681a5 | 2013-03-15 09:00:29 -0400 | [diff] [blame] | 526 | * @id: bandgap sensor id |
| 527 | * @val: value (mCelsius) of a threshold |
| 528 | * @hot: desired threshold to be read. true if threshold hot, false if |
| 529 | * threshold cold |
| 530 | * |
| 531 | * It will fetch the required thresholds (hot and cold) for TALERT signal. |
| 532 | * This function can be used to read t_hot or t_cold, depending on @hot value. |
| 533 | * Call this function only if bandgap features HAS(TALERT). |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 534 | * |
| 535 | * Return: 0 if no error, -ENOTSUPP if it has no TALERT support, or the |
| 536 | * corresponding error value if some operation fails. |
Eduardo Valentin | 7a681a5 | 2013-03-15 09:00:29 -0400 | [diff] [blame] | 537 | */ |
Eduardo Valentin | 2f8ec2a | 2013-03-19 10:54:22 -0400 | [diff] [blame] | 538 | static int _ti_bandgap_read_threshold(struct ti_bandgap *bgp, int id, |
| 539 | int *val, bool hot) |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 540 | { |
| 541 | struct temp_sensor_registers *tsr; |
| 542 | u32 temp, mask; |
| 543 | int ret = 0; |
| 544 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 545 | ret = ti_bandgap_validate(bgp, id); |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 546 | if (ret) |
| 547 | goto exit; |
| 548 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 549 | if (!TI_BANDGAP_HAS(bgp, TALERT)) { |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 550 | ret = -ENOTSUPP; |
| 551 | goto exit; |
| 552 | } |
| 553 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 554 | tsr = bgp->conf->sensors[id].registers; |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 555 | if (hot) |
| 556 | mask = tsr->threshold_thot_mask; |
| 557 | else |
| 558 | mask = tsr->threshold_tcold_mask; |
| 559 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 560 | temp = ti_bandgap_readl(bgp, tsr->bgap_threshold); |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 561 | temp = (temp & mask) >> __ffs(mask); |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 562 | ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp); |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 563 | if (ret) { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 564 | dev_err(bgp->dev, "failed to read thot\n"); |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 565 | ret = -EIO; |
| 566 | goto exit; |
| 567 | } |
| 568 | |
| 569 | *val = temp; |
| 570 | |
| 571 | exit: |
Eduardo Valentin | 648b4c6 | 2013-03-19 10:54:17 -0400 | [diff] [blame] | 572 | return ret; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 573 | } |
| 574 | |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 575 | /*** Exposed APIs ***/ |
| 576 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 577 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 578 | * ti_bandgap_read_thot() - reads sensor current thot |
Eduardo Valentin | 61603af | 2013-03-19 10:54:25 -0400 | [diff] [blame] | 579 | * @bgp: pointer to bandgap instance |
| 580 | * @id: sensor id |
| 581 | * @thot: resulting current thot value |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 582 | * |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 583 | * Return: 0 on success or the proper error code |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 584 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 585 | int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 586 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 587 | return _ti_bandgap_read_threshold(bgp, id, thot, true); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 591 | * ti_bandgap_write_thot() - sets sensor current thot |
Eduardo Valentin | 61603af | 2013-03-19 10:54:25 -0400 | [diff] [blame] | 592 | * @bgp: pointer to bandgap instance |
| 593 | * @id: sensor id |
| 594 | * @val: desired thot value |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 595 | * |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 596 | * Return: 0 on success or the proper error code |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 597 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 598 | int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 599 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 600 | return _ti_bandgap_write_threshold(bgp, id, val, true); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 604 | * ti_bandgap_read_tcold() - reads sensor current tcold |
Eduardo Valentin | 61603af | 2013-03-19 10:54:25 -0400 | [diff] [blame] | 605 | * @bgp: pointer to bandgap instance |
| 606 | * @id: sensor id |
| 607 | * @tcold: resulting current tcold value |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 608 | * |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 609 | * Return: 0 on success or the proper error code |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 610 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 611 | int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 612 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 613 | return _ti_bandgap_read_threshold(bgp, id, tcold, false); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 617 | * ti_bandgap_write_tcold() - sets the sensor tcold |
Eduardo Valentin | 61603af | 2013-03-19 10:54:25 -0400 | [diff] [blame] | 618 | * @bgp: pointer to bandgap instance |
| 619 | * @id: sensor id |
| 620 | * @val: desired tcold value |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 621 | * |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 622 | * Return: 0 on success or the proper error code |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 623 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 624 | int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 625 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 626 | return _ti_bandgap_write_threshold(bgp, id, val, false); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /** |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 630 | * ti_bandgap_read_counter() - read the sensor counter |
| 631 | * @bgp: pointer to bandgap instance |
| 632 | * @id: sensor id |
| 633 | * @interval: resulting update interval in miliseconds |
| 634 | */ |
| 635 | static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id, |
| 636 | int *interval) |
| 637 | { |
| 638 | struct temp_sensor_registers *tsr; |
| 639 | int time; |
| 640 | |
| 641 | tsr = bgp->conf->sensors[id].registers; |
| 642 | time = ti_bandgap_readl(bgp, tsr->bgap_counter); |
| 643 | time = (time & tsr->counter_mask) >> |
| 644 | __ffs(tsr->counter_mask); |
| 645 | time = time * 1000 / bgp->clk_rate; |
| 646 | *interval = time; |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * ti_bandgap_read_counter_delay() - read the sensor counter delay |
| 651 | * @bgp: pointer to bandgap instance |
| 652 | * @id: sensor id |
| 653 | * @interval: resulting update interval in miliseconds |
| 654 | */ |
| 655 | static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id, |
| 656 | int *interval) |
| 657 | { |
| 658 | struct temp_sensor_registers *tsr; |
| 659 | int reg_val; |
| 660 | |
| 661 | tsr = bgp->conf->sensors[id].registers; |
| 662 | |
| 663 | reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl); |
| 664 | reg_val = (reg_val & tsr->mask_counter_delay_mask) >> |
| 665 | __ffs(tsr->mask_counter_delay_mask); |
| 666 | switch (reg_val) { |
| 667 | case 0: |
| 668 | *interval = 0; |
| 669 | break; |
| 670 | case 1: |
| 671 | *interval = 1; |
| 672 | break; |
| 673 | case 2: |
| 674 | *interval = 10; |
| 675 | break; |
| 676 | case 3: |
| 677 | *interval = 100; |
| 678 | break; |
| 679 | case 4: |
| 680 | *interval = 250; |
| 681 | break; |
| 682 | case 5: |
| 683 | *interval = 500; |
| 684 | break; |
| 685 | default: |
| 686 | dev_warn(bgp->dev, "Wrong counter delay value read from register %X", |
| 687 | reg_val); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 692 | * ti_bandgap_read_update_interval() - read the sensor update interval |
Eduardo Valentin | 61603af | 2013-03-19 10:54:25 -0400 | [diff] [blame] | 693 | * @bgp: pointer to bandgap instance |
| 694 | * @id: sensor id |
| 695 | * @interval: resulting update interval in miliseconds |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 696 | * |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 697 | * Return: 0 on success or the proper error code |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 698 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 699 | int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id, |
| 700 | int *interval) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 701 | { |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 702 | int ret = 0; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 703 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 704 | ret = ti_bandgap_validate(bgp, id); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 705 | if (ret) |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 706 | goto exit; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 707 | |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 708 | if (!TI_BANDGAP_HAS(bgp, COUNTER) && |
| 709 | !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) { |
| 710 | ret = -ENOTSUPP; |
| 711 | goto exit; |
| 712 | } |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 713 | |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 714 | if (TI_BANDGAP_HAS(bgp, COUNTER)) { |
| 715 | ti_bandgap_read_counter(bgp, id, interval); |
| 716 | goto exit; |
| 717 | } |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 718 | |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 719 | ti_bandgap_read_counter_delay(bgp, id, interval); |
| 720 | exit: |
| 721 | return ret; |
| 722 | } |
| 723 | |
| 724 | /** |
| 725 | * ti_bandgap_write_counter_delay() - set the counter_delay |
| 726 | * @bgp: pointer to bandgap instance |
| 727 | * @id: sensor id |
| 728 | * @interval: desired update interval in miliseconds |
| 729 | * |
| 730 | * Return: 0 on success or the proper error code |
| 731 | */ |
| 732 | static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id, |
| 733 | u32 interval) |
| 734 | { |
| 735 | int rval; |
| 736 | |
| 737 | switch (interval) { |
| 738 | case 0: /* Immediate conversion */ |
| 739 | rval = 0x0; |
| 740 | break; |
| 741 | case 1: /* Conversion after ever 1ms */ |
| 742 | rval = 0x1; |
| 743 | break; |
| 744 | case 10: /* Conversion after ever 10ms */ |
| 745 | rval = 0x2; |
| 746 | break; |
| 747 | case 100: /* Conversion after ever 100ms */ |
| 748 | rval = 0x3; |
| 749 | break; |
| 750 | case 250: /* Conversion after ever 250ms */ |
| 751 | rval = 0x4; |
| 752 | break; |
| 753 | case 500: /* Conversion after ever 500ms */ |
| 754 | rval = 0x5; |
| 755 | break; |
| 756 | default: |
| 757 | dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval); |
| 758 | return -EINVAL; |
| 759 | } |
| 760 | |
| 761 | spin_lock(&bgp->lock); |
| 762 | RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval); |
| 763 | spin_unlock(&bgp->lock); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 764 | |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | /** |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 769 | * ti_bandgap_write_counter() - set the bandgap sensor counter |
| 770 | * @bgp: pointer to bandgap instance |
| 771 | * @id: sensor id |
| 772 | * @interval: desired update interval in miliseconds |
| 773 | */ |
| 774 | static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id, |
| 775 | u32 interval) |
| 776 | { |
| 777 | interval = interval * bgp->clk_rate / 1000; |
| 778 | spin_lock(&bgp->lock); |
| 779 | RMW_BITS(bgp, id, bgap_counter, counter_mask, interval); |
| 780 | spin_unlock(&bgp->lock); |
| 781 | } |
| 782 | |
| 783 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 784 | * ti_bandgap_write_update_interval() - set the update interval |
Eduardo Valentin | 61603af | 2013-03-19 10:54:25 -0400 | [diff] [blame] | 785 | * @bgp: pointer to bandgap instance |
| 786 | * @id: sensor id |
| 787 | * @interval: desired update interval in miliseconds |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 788 | * |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 789 | * Return: 0 on success or the proper error code |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 790 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 791 | int ti_bandgap_write_update_interval(struct ti_bandgap *bgp, |
| 792 | int id, u32 interval) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 793 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 794 | int ret = ti_bandgap_validate(bgp, id); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 795 | if (ret) |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 796 | goto exit; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 797 | |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 798 | if (!TI_BANDGAP_HAS(bgp, COUNTER) && |
| 799 | !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) { |
| 800 | ret = -ENOTSUPP; |
| 801 | goto exit; |
| 802 | } |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 803 | |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 804 | if (TI_BANDGAP_HAS(bgp, COUNTER)) { |
| 805 | ti_bandgap_write_counter(bgp, id, interval); |
| 806 | goto exit; |
| 807 | } |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 808 | |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 809 | ret = ti_bandgap_write_counter_delay(bgp, id, interval); |
| 810 | exit: |
| 811 | return ret; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 815 | * ti_bandgap_read_temperature() - report current temperature |
Eduardo Valentin | 61603af | 2013-03-19 10:54:25 -0400 | [diff] [blame] | 816 | * @bgp: pointer to bandgap instance |
| 817 | * @id: sensor id |
| 818 | * @temperature: resulting temperature |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 819 | * |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 820 | * Return: 0 on success or the proper error code |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 821 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 822 | int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id, |
| 823 | int *temperature) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 824 | { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 825 | u32 temp; |
| 826 | int ret; |
| 827 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 828 | ret = ti_bandgap_validate(bgp, id); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 829 | if (ret) |
| 830 | return ret; |
| 831 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 832 | spin_lock(&bgp->lock); |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 833 | temp = ti_bandgap_read_temp(bgp, id); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 834 | spin_unlock(&bgp->lock); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 835 | |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 836 | ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 837 | if (ret) |
| 838 | return -EIO; |
| 839 | |
| 840 | *temperature = temp; |
| 841 | |
| 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 846 | * ti_bandgap_set_sensor_data() - helper function to store thermal |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 847 | * framework related data. |
Eduardo Valentin | 61603af | 2013-03-19 10:54:25 -0400 | [diff] [blame] | 848 | * @bgp: pointer to bandgap instance |
| 849 | * @id: sensor id |
| 850 | * @data: thermal framework related data to be stored |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 851 | * |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 852 | * Return: 0 on success or the proper error code |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 853 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 854 | int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 855 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 856 | int ret = ti_bandgap_validate(bgp, id); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 857 | if (ret) |
| 858 | return ret; |
| 859 | |
Eduardo Valentin | 9879b2c | 2013-03-19 10:54:23 -0400 | [diff] [blame] | 860 | bgp->regval[id].data = data; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 861 | |
| 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 866 | * ti_bandgap_get_sensor_data() - helper function to get thermal |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 867 | * framework related data. |
Eduardo Valentin | 61603af | 2013-03-19 10:54:25 -0400 | [diff] [blame] | 868 | * @bgp: pointer to bandgap instance |
| 869 | * @id: sensor id |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 870 | * |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 871 | * Return: data stored by set function with sensor id on success or NULL |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 872 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 873 | void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 874 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 875 | int ret = ti_bandgap_validate(bgp, id); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 876 | if (ret) |
| 877 | return ERR_PTR(ret); |
| 878 | |
Eduardo Valentin | 9879b2c | 2013-03-19 10:54:23 -0400 | [diff] [blame] | 879 | return bgp->regval[id].data; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 880 | } |
| 881 | |
Eduardo Valentin | e195aba | 2013-03-15 09:00:22 -0400 | [diff] [blame] | 882 | /*** Helper functions used during device initialization ***/ |
| 883 | |
Eduardo Valentin | 31102a7 | 2013-03-15 09:00:26 -0400 | [diff] [blame] | 884 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 885 | * ti_bandgap_force_single_read() - executes 1 single ADC conversion |
| 886 | * @bgp: pointer to struct ti_bandgap |
Eduardo Valentin | 31102a7 | 2013-03-15 09:00:26 -0400 | [diff] [blame] | 887 | * @id: sensor id which it is desired to read 1 temperature |
| 888 | * |
| 889 | * Used to initialize the conversion state machine and set it to a valid |
| 890 | * state. Called during device initialization and context restore events. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 891 | * |
| 892 | * Return: 0 |
Eduardo Valentin | 31102a7 | 2013-03-15 09:00:26 -0400 | [diff] [blame] | 893 | */ |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 894 | static int |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 895 | ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 896 | { |
Pavel Machek | a4296d1 | 2015-01-18 21:20:51 +0100 | [diff] [blame^] | 897 | u32 counter = 1000; |
| 898 | struct temp_sensor_registers *tsr; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 899 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 900 | /* Select single conversion mode */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 901 | if (TI_BANDGAP_HAS(bgp, MODE_CONFIG)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 902 | RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 903 | |
| 904 | /* Start of Conversion = 1 */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 905 | RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1); |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 906 | |
Pavel Machek | a4296d1 | 2015-01-18 21:20:51 +0100 | [diff] [blame^] | 907 | /* Wait for EOCZ going up */ |
| 908 | tsr = bgp->conf->sensors[id].registers; |
| 909 | |
| 910 | while (--counter) { |
| 911 | if (ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) & |
| 912 | tsr->bgap_eocz_mask) |
| 913 | break; |
| 914 | } |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 915 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 916 | /* Start of Conversion = 0 */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 917 | RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 918 | |
Pavel Machek | a4296d1 | 2015-01-18 21:20:51 +0100 | [diff] [blame^] | 919 | /* Wait for EOCZ going down */ |
| 920 | counter = 1000; |
| 921 | while (--counter) { |
| 922 | if (!(ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) & |
| 923 | tsr->bgap_eocz_mask)) |
| 924 | break; |
| 925 | } |
| 926 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 927 | return 0; |
| 928 | } |
| 929 | |
| 930 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 931 | * ti_bandgap_set_continous_mode() - One time enabling of continuous mode |
| 932 | * @bgp: pointer to struct ti_bandgap |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 933 | * |
Eduardo Valentin | a84b6f4 | 2013-03-15 09:00:25 -0400 | [diff] [blame] | 934 | * Call this function only if HAS(MODE_CONFIG) is set. As this driver may |
| 935 | * be used for junction temperature monitoring, it is desirable that the |
| 936 | * sensors are operational all the time, so that alerts are generated |
| 937 | * properly. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 938 | * |
| 939 | * Return: 0 |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 940 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 941 | static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 942 | { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 943 | int i; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 944 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 945 | for (i = 0; i < bgp->conf->sensor_count; i++) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 946 | /* Perform a single read just before enabling continuous */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 947 | ti_bandgap_force_single_read(bgp, i); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 948 | RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | return 0; |
| 952 | } |
| 953 | |
Eduardo Valentin | d3790b3 | 2013-03-15 09:00:30 -0400 | [diff] [blame] | 954 | /** |
J Keerthy | 2f440b0 | 2013-04-01 12:04:45 -0400 | [diff] [blame] | 955 | * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor |
| 956 | * @bgp: pointer to struct ti_bandgap |
| 957 | * @id: id of the individual sensor |
| 958 | * @trend: Pointer to trend. |
| 959 | * |
| 960 | * This function needs to be called to fetch the temperature trend of a |
| 961 | * Particular sensor. The function computes the difference in temperature |
| 962 | * w.r.t time. For the bandgaps with built in history buffer the temperatures |
| 963 | * are read from the buffer and for those without the Buffer -ENOTSUPP is |
| 964 | * returned. |
| 965 | * |
| 966 | * Return: 0 if no error, else return corresponding error. If no |
| 967 | * error then the trend value is passed on to trend parameter |
| 968 | */ |
| 969 | int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend) |
| 970 | { |
| 971 | struct temp_sensor_registers *tsr; |
| 972 | u32 temp1, temp2, reg1, reg2; |
| 973 | int t1, t2, interval, ret = 0; |
| 974 | |
| 975 | ret = ti_bandgap_validate(bgp, id); |
| 976 | if (ret) |
| 977 | goto exit; |
| 978 | |
| 979 | if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) || |
| 980 | !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) { |
| 981 | ret = -ENOTSUPP; |
| 982 | goto exit; |
| 983 | } |
| 984 | |
Eduardo Valentin | ba0049e | 2013-06-07 19:13:13 +0000 | [diff] [blame] | 985 | spin_lock(&bgp->lock); |
| 986 | |
J Keerthy | 2f440b0 | 2013-04-01 12:04:45 -0400 | [diff] [blame] | 987 | tsr = bgp->conf->sensors[id].registers; |
| 988 | |
| 989 | /* Freeze and read the last 2 valid readings */ |
Eduardo Valentin | ba0049e | 2013-06-07 19:13:13 +0000 | [diff] [blame] | 990 | RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1); |
J Keerthy | 2f440b0 | 2013-04-01 12:04:45 -0400 | [diff] [blame] | 991 | reg1 = tsr->ctrl_dtemp_1; |
| 992 | reg2 = tsr->ctrl_dtemp_2; |
| 993 | |
| 994 | /* read temperature from history buffer */ |
| 995 | temp1 = ti_bandgap_readl(bgp, reg1); |
| 996 | temp1 &= tsr->bgap_dtemp_mask; |
| 997 | |
| 998 | temp2 = ti_bandgap_readl(bgp, reg2); |
| 999 | temp2 &= tsr->bgap_dtemp_mask; |
| 1000 | |
| 1001 | /* Convert from adc values to mCelsius temperature */ |
| 1002 | ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1); |
| 1003 | if (ret) |
Eduardo Valentin | ba0049e | 2013-06-07 19:13:13 +0000 | [diff] [blame] | 1004 | goto unfreeze; |
J Keerthy | 2f440b0 | 2013-04-01 12:04:45 -0400 | [diff] [blame] | 1005 | |
| 1006 | ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2); |
| 1007 | if (ret) |
Eduardo Valentin | ba0049e | 2013-06-07 19:13:13 +0000 | [diff] [blame] | 1008 | goto unfreeze; |
J Keerthy | 2f440b0 | 2013-04-01 12:04:45 -0400 | [diff] [blame] | 1009 | |
| 1010 | /* Fetch the update interval */ |
| 1011 | ret = ti_bandgap_read_update_interval(bgp, id, &interval); |
Ranganath Krishnan | e838ff8 | 2013-08-23 11:08:23 -0500 | [diff] [blame] | 1012 | if (ret) |
Eduardo Valentin | ba0049e | 2013-06-07 19:13:13 +0000 | [diff] [blame] | 1013 | goto unfreeze; |
J Keerthy | 2f440b0 | 2013-04-01 12:04:45 -0400 | [diff] [blame] | 1014 | |
Ranganath Krishnan | e838ff8 | 2013-08-23 11:08:23 -0500 | [diff] [blame] | 1015 | /* Set the interval to 1 ms if bandgap counter delay is not set */ |
| 1016 | if (interval == 0) |
| 1017 | interval = 1; |
| 1018 | |
J Keerthy | 2f440b0 | 2013-04-01 12:04:45 -0400 | [diff] [blame] | 1019 | *trend = (t1 - t2) / interval; |
| 1020 | |
| 1021 | dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n", |
| 1022 | t1, t2, *trend); |
| 1023 | |
Eduardo Valentin | ba0049e | 2013-06-07 19:13:13 +0000 | [diff] [blame] | 1024 | unfreeze: |
| 1025 | RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0); |
| 1026 | spin_unlock(&bgp->lock); |
J Keerthy | 2f440b0 | 2013-04-01 12:04:45 -0400 | [diff] [blame] | 1027 | exit: |
| 1028 | return ret; |
| 1029 | } |
| 1030 | |
| 1031 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1032 | * ti_bandgap_tshut_init() - setup and initialize tshut handling |
| 1033 | * @bgp: pointer to struct ti_bandgap |
Eduardo Valentin | d3790b3 | 2013-03-15 09:00:30 -0400 | [diff] [blame] | 1034 | * @pdev: pointer to device struct platform_device |
| 1035 | * |
| 1036 | * Call this function only in case the bandgap features HAS(TSHUT). |
| 1037 | * In this case, the driver needs to handle the TSHUT signal as an IRQ. |
| 1038 | * The IRQ is wired as a GPIO, and for this purpose, it is required |
| 1039 | * to specify which GPIO line is used. TSHUT IRQ is fired anytime |
| 1040 | * one of the bandgap sensors violates the TSHUT high/hot threshold. |
| 1041 | * And in that case, the system must go off. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 1042 | * |
| 1043 | * Return: 0 if no error, else error status |
Eduardo Valentin | d3790b3 | 2013-03-15 09:00:30 -0400 | [diff] [blame] | 1044 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1045 | static int ti_bandgap_tshut_init(struct ti_bandgap *bgp, |
| 1046 | struct platform_device *pdev) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1047 | { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1048 | int gpio_nr = bgp->tshut_gpio; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1049 | int status; |
| 1050 | |
| 1051 | /* Request for gpio_86 line */ |
| 1052 | status = gpio_request(gpio_nr, "tshut"); |
| 1053 | if (status < 0) { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1054 | dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1055 | return status; |
| 1056 | } |
| 1057 | status = gpio_direction_input(gpio_nr); |
| 1058 | if (status) { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1059 | dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1060 | return status; |
| 1061 | } |
| 1062 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1063 | status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler, |
| 1064 | IRQF_TRIGGER_RISING, "tshut", NULL); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1065 | if (status) { |
| 1066 | gpio_free(gpio_nr); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1067 | dev_err(bgp->dev, "request irq failed for TSHUT"); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
Eduardo Valentin | 094b8ac | 2013-03-15 09:00:31 -0400 | [diff] [blame] | 1073 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1074 | * ti_bandgap_alert_init() - setup and initialize talert handling |
| 1075 | * @bgp: pointer to struct ti_bandgap |
Eduardo Valentin | 094b8ac | 2013-03-15 09:00:31 -0400 | [diff] [blame] | 1076 | * @pdev: pointer to device struct platform_device |
| 1077 | * |
| 1078 | * Call this function only in case the bandgap features HAS(TALERT). |
| 1079 | * In this case, the driver needs to handle the TALERT signals as an IRQs. |
| 1080 | * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold) |
| 1081 | * are violated. In these situation, the driver must reprogram the thresholds, |
| 1082 | * accordingly to specified policy. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 1083 | * |
| 1084 | * Return: 0 if no error, else return corresponding error. |
Eduardo Valentin | 094b8ac | 2013-03-15 09:00:31 -0400 | [diff] [blame] | 1085 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1086 | static int ti_bandgap_talert_init(struct ti_bandgap *bgp, |
| 1087 | struct platform_device *pdev) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1088 | { |
| 1089 | int ret; |
| 1090 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1091 | bgp->irq = platform_get_irq(pdev, 0); |
| 1092 | if (bgp->irq < 0) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1093 | dev_err(&pdev->dev, "get_irq failed\n"); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1094 | return bgp->irq; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1095 | } |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1096 | ret = request_threaded_irq(bgp->irq, NULL, |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1097 | ti_bandgap_talert_irq_handler, |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1098 | IRQF_TRIGGER_HIGH | IRQF_ONESHOT, |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1099 | "talert", bgp); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1100 | if (ret) { |
| 1101 | dev_err(&pdev->dev, "Request threaded irq failed.\n"); |
| 1102 | return ret; |
| 1103 | } |
| 1104 | |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
Eduardo Valentin | 61603af | 2013-03-19 10:54:25 -0400 | [diff] [blame] | 1108 | static const struct of_device_id of_ti_bandgap_match[]; |
Eduardo Valentin | e9b6f8c | 2013-03-15 09:00:32 -0400 | [diff] [blame] | 1109 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1110 | * ti_bandgap_build() - parse DT and setup a struct ti_bandgap |
Eduardo Valentin | e9b6f8c | 2013-03-15 09:00:32 -0400 | [diff] [blame] | 1111 | * @pdev: pointer to device struct platform_device |
| 1112 | * |
| 1113 | * Used to read the device tree properties accordingly to the bandgap |
| 1114 | * matching version. Based on bandgap version and its capabilities it |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1115 | * will build a struct ti_bandgap out of the required DT entries. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 1116 | * |
| 1117 | * Return: valid bandgap structure if successful, else returns ERR_PTR |
| 1118 | * return value must be verified with IS_ERR. |
Eduardo Valentin | e9b6f8c | 2013-03-15 09:00:32 -0400 | [diff] [blame] | 1119 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1120 | static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1121 | { |
| 1122 | struct device_node *node = pdev->dev.of_node; |
| 1123 | const struct of_device_id *of_id; |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1124 | struct ti_bandgap *bgp; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1125 | struct resource *res; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1126 | int i; |
| 1127 | |
| 1128 | /* just for the sake */ |
| 1129 | if (!node) { |
| 1130 | dev_err(&pdev->dev, "no platform information available\n"); |
| 1131 | return ERR_PTR(-EINVAL); |
| 1132 | } |
| 1133 | |
Eduardo Valentin | f684356 | 2013-03-19 10:54:24 -0400 | [diff] [blame] | 1134 | bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1135 | if (!bgp) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1136 | dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n"); |
| 1137 | return ERR_PTR(-ENOMEM); |
| 1138 | } |
| 1139 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1140 | of_id = of_match_device(of_ti_bandgap_match, &pdev->dev); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1141 | if (of_id) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1142 | bgp->conf = of_id->data; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1143 | |
Eduardo Valentin | 9879b2c | 2013-03-19 10:54:23 -0400 | [diff] [blame] | 1144 | /* register shadow for context save and restore */ |
| 1145 | bgp->regval = devm_kzalloc(&pdev->dev, sizeof(*bgp->regval) * |
| 1146 | bgp->conf->sensor_count, GFP_KERNEL); |
Rickard Strandqvist | fbe2ddc | 2014-06-02 23:25:30 +0200 | [diff] [blame] | 1147 | if (!bgp->regval) { |
Eduardo Valentin | 9879b2c | 2013-03-19 10:54:23 -0400 | [diff] [blame] | 1148 | dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n"); |
| 1149 | return ERR_PTR(-ENOMEM); |
| 1150 | } |
| 1151 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1152 | i = 0; |
| 1153 | do { |
| 1154 | void __iomem *chunk; |
| 1155 | |
| 1156 | res = platform_get_resource(pdev, IORESOURCE_MEM, i); |
| 1157 | if (!res) |
| 1158 | break; |
Thierry Reding | 97f4be60 | 2013-01-21 11:09:19 +0100 | [diff] [blame] | 1159 | chunk = devm_ioremap_resource(&pdev->dev, res); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1160 | if (i == 0) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1161 | bgp->base = chunk; |
Thierry Reding | 97f4be60 | 2013-01-21 11:09:19 +0100 | [diff] [blame] | 1162 | if (IS_ERR(chunk)) |
| 1163 | return ERR_CAST(chunk); |
Eduardo Valentin | 24796e1 | 2013-03-15 08:59:53 -0400 | [diff] [blame] | 1164 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1165 | i++; |
| 1166 | } while (res); |
| 1167 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1168 | if (TI_BANDGAP_HAS(bgp, TSHUT)) { |
Eduardo Valentin | 57d1617 | 2013-06-07 11:11:53 -0400 | [diff] [blame] | 1169 | bgp->tshut_gpio = of_get_gpio(node, 0); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1170 | if (!gpio_is_valid(bgp->tshut_gpio)) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1171 | dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n", |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1172 | bgp->tshut_gpio); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1173 | return ERR_PTR(-EINVAL); |
| 1174 | } |
| 1175 | } |
| 1176 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1177 | return bgp; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1178 | } |
| 1179 | |
Eduardo Valentin | f91ddfe | 2013-03-15 09:00:23 -0400 | [diff] [blame] | 1180 | /*** Device driver call backs ***/ |
| 1181 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1182 | static |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1183 | int ti_bandgap_probe(struct platform_device *pdev) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1184 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1185 | struct ti_bandgap *bgp; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1186 | int clk_rate, ret = 0, i; |
| 1187 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1188 | bgp = ti_bandgap_build(pdev); |
Eduardo Valentin | 0c12b5a | 2013-05-29 15:07:43 +0000 | [diff] [blame] | 1189 | if (IS_ERR(bgp)) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1190 | dev_err(&pdev->dev, "failed to fetch platform data\n"); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1191 | return PTR_ERR(bgp); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1192 | } |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1193 | bgp->dev = &pdev->dev; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1194 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1195 | if (TI_BANDGAP_HAS(bgp, TSHUT)) { |
| 1196 | ret = ti_bandgap_tshut_init(bgp, pdev); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1197 | if (ret) { |
| 1198 | dev_err(&pdev->dev, |
| 1199 | "failed to initialize system tshut IRQ\n"); |
| 1200 | return ret; |
| 1201 | } |
| 1202 | } |
| 1203 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1204 | bgp->fclock = clk_get(NULL, bgp->conf->fclock_name); |
Eduardo Valentin | 0c12b5a | 2013-05-29 15:07:43 +0000 | [diff] [blame] | 1205 | ret = IS_ERR(bgp->fclock); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1206 | if (ret) { |
| 1207 | dev_err(&pdev->dev, "failed to request fclock reference\n"); |
Eduardo Valentin | 0c12b5a | 2013-05-29 15:07:43 +0000 | [diff] [blame] | 1208 | ret = PTR_ERR(bgp->fclock); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1209 | goto free_irqs; |
| 1210 | } |
| 1211 | |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 1212 | bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name); |
Eduardo Valentin | 0c12b5a | 2013-05-29 15:07:43 +0000 | [diff] [blame] | 1213 | ret = IS_ERR(bgp->div_clk); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1214 | if (ret) { |
Pavel Machek | e34238b | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 1215 | dev_err(&pdev->dev, "failed to request div_ts_ck clock ref\n"); |
Eduardo Valentin | 0c12b5a | 2013-05-29 15:07:43 +0000 | [diff] [blame] | 1216 | ret = PTR_ERR(bgp->div_clk); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1217 | goto free_irqs; |
| 1218 | } |
| 1219 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1220 | for (i = 0; i < bgp->conf->sensor_count; i++) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1221 | struct temp_sensor_registers *tsr; |
| 1222 | u32 val; |
| 1223 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1224 | tsr = bgp->conf->sensors[i].registers; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1225 | /* |
| 1226 | * check if the efuse has a non-zero value if not |
| 1227 | * it is an untrimmed sample and the temperatures |
| 1228 | * may not be accurate |
| 1229 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1230 | val = ti_bandgap_readl(bgp, tsr->bgap_efuse); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1231 | if (ret || !val) |
| 1232 | dev_info(&pdev->dev, |
| 1233 | "Non-trimmed BGAP, Temp not accurate\n"); |
| 1234 | } |
| 1235 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1236 | clk_rate = clk_round_rate(bgp->div_clk, |
| 1237 | bgp->conf->sensors[0].ts_data->max_freq); |
| 1238 | if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq || |
Paul Walmsley | c68789e | 2013-12-09 18:09:22 -0800 | [diff] [blame] | 1239 | clk_rate <= 0) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1240 | ret = -ENODEV; |
| 1241 | dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate); |
| 1242 | goto put_clks; |
| 1243 | } |
| 1244 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1245 | ret = clk_set_rate(bgp->div_clk, clk_rate); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1246 | if (ret) |
| 1247 | dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n"); |
| 1248 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1249 | bgp->clk_rate = clk_rate; |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1250 | if (TI_BANDGAP_HAS(bgp, CLK_CTRL)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1251 | clk_prepare_enable(bgp->fclock); |
Radhesh Fadnis | 6c9c1d6 | 2013-02-26 18:53:25 -0400 | [diff] [blame] | 1252 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1253 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1254 | spin_lock_init(&bgp->lock); |
| 1255 | bgp->dev = &pdev->dev; |
| 1256 | platform_set_drvdata(pdev, bgp); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1257 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1258 | ti_bandgap_power(bgp, true); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1259 | |
| 1260 | /* Set default counter to 1 for now */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1261 | if (TI_BANDGAP_HAS(bgp, COUNTER)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1262 | for (i = 0; i < bgp->conf->sensor_count; i++) |
| 1263 | RMW_BITS(bgp, i, bgap_counter, counter_mask, 1); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1264 | |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 1265 | /* Set default thresholds for alert and shutdown */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1266 | for (i = 0; i < bgp->conf->sensor_count; i++) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1267 | struct temp_sensor_data *ts_data; |
| 1268 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1269 | ts_data = bgp->conf->sensors[i].ts_data; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1270 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1271 | if (TI_BANDGAP_HAS(bgp, TALERT)) { |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 1272 | /* Set initial Talert thresholds */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1273 | RMW_BITS(bgp, i, bgap_threshold, |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 1274 | threshold_tcold_mask, ts_data->t_cold); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1275 | RMW_BITS(bgp, i, bgap_threshold, |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 1276 | threshold_thot_mask, ts_data->t_hot); |
| 1277 | /* Enable the alert events */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1278 | RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1); |
| 1279 | RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1); |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 1280 | } |
| 1281 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1282 | if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) { |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 1283 | /* Set initial Tshut thresholds */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1284 | RMW_BITS(bgp, i, tshut_threshold, |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 1285 | tshut_hot_mask, ts_data->tshut_hot); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1286 | RMW_BITS(bgp, i, tshut_threshold, |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 1287 | tshut_cold_mask, ts_data->tshut_cold); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1288 | } |
| 1289 | } |
| 1290 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1291 | if (TI_BANDGAP_HAS(bgp, MODE_CONFIG)) |
| 1292 | ti_bandgap_set_continuous_mode(bgp); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1293 | |
| 1294 | /* Set .250 seconds time as default counter */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1295 | if (TI_BANDGAP_HAS(bgp, COUNTER)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1296 | for (i = 0; i < bgp->conf->sensor_count; i++) |
| 1297 | RMW_BITS(bgp, i, bgap_counter, counter_mask, |
| 1298 | bgp->clk_rate / 4); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1299 | |
| 1300 | /* Every thing is good? Then expose the sensors */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1301 | for (i = 0; i < bgp->conf->sensor_count; i++) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1302 | char *domain; |
| 1303 | |
Eduardo Valentin | f155333 | 2013-04-08 08:19:13 -0400 | [diff] [blame] | 1304 | if (bgp->conf->sensors[i].register_cooling) { |
| 1305 | ret = bgp->conf->sensors[i].register_cooling(bgp, i); |
| 1306 | if (ret) |
| 1307 | goto remove_sensors; |
| 1308 | } |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 1309 | |
Eduardo Valentin | f155333 | 2013-04-08 08:19:13 -0400 | [diff] [blame] | 1310 | if (bgp->conf->expose_sensor) { |
| 1311 | domain = bgp->conf->sensors[i].domain; |
| 1312 | ret = bgp->conf->expose_sensor(bgp, i, domain); |
| 1313 | if (ret) |
| 1314 | goto remove_last_cooling; |
| 1315 | } |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | /* |
| 1319 | * Enable the Interrupts once everything is set. Otherwise irq handler |
| 1320 | * might be called as soon as it is enabled where as rest of framework |
| 1321 | * is still getting initialised. |
| 1322 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1323 | if (TI_BANDGAP_HAS(bgp, TALERT)) { |
| 1324 | ret = ti_bandgap_talert_init(bgp, pdev); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1325 | if (ret) { |
| 1326 | dev_err(&pdev->dev, "failed to initialize Talert IRQ\n"); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1327 | i = bgp->conf->sensor_count; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1328 | goto disable_clk; |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | return 0; |
| 1333 | |
Eduardo Valentin | f155333 | 2013-04-08 08:19:13 -0400 | [diff] [blame] | 1334 | remove_last_cooling: |
| 1335 | if (bgp->conf->sensors[i].unregister_cooling) |
| 1336 | bgp->conf->sensors[i].unregister_cooling(bgp, i); |
| 1337 | remove_sensors: |
| 1338 | for (i--; i >= 0; i--) { |
| 1339 | if (bgp->conf->sensors[i].unregister_cooling) |
| 1340 | bgp->conf->sensors[i].unregister_cooling(bgp, i); |
| 1341 | if (bgp->conf->remove_sensor) |
| 1342 | bgp->conf->remove_sensor(bgp, i); |
| 1343 | } |
| 1344 | ti_bandgap_power(bgp, false); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1345 | disable_clk: |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1346 | if (TI_BANDGAP_HAS(bgp, CLK_CTRL)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1347 | clk_disable_unprepare(bgp->fclock); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1348 | put_clks: |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1349 | clk_put(bgp->fclock); |
| 1350 | clk_put(bgp->div_clk); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1351 | free_irqs: |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1352 | if (TI_BANDGAP_HAS(bgp, TSHUT)) { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1353 | free_irq(gpio_to_irq(bgp->tshut_gpio), NULL); |
| 1354 | gpio_free(bgp->tshut_gpio); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | return ret; |
| 1358 | } |
| 1359 | |
| 1360 | static |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1361 | int ti_bandgap_remove(struct platform_device *pdev) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1362 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1363 | struct ti_bandgap *bgp = platform_get_drvdata(pdev); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1364 | int i; |
| 1365 | |
| 1366 | /* First thing is to remove sensor interfaces */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1367 | for (i = 0; i < bgp->conf->sensor_count; i++) { |
Eduardo Valentin | 262235b | 2013-04-08 08:19:14 -0400 | [diff] [blame] | 1368 | if (bgp->conf->sensors[i].unregister_cooling) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1369 | bgp->conf->sensors[i].unregister_cooling(bgp, i); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1370 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1371 | if (bgp->conf->remove_sensor) |
| 1372 | bgp->conf->remove_sensor(bgp, i); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1373 | } |
| 1374 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1375 | ti_bandgap_power(bgp, false); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1376 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1377 | if (TI_BANDGAP_HAS(bgp, CLK_CTRL)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1378 | clk_disable_unprepare(bgp->fclock); |
| 1379 | clk_put(bgp->fclock); |
| 1380 | clk_put(bgp->div_clk); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1381 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1382 | if (TI_BANDGAP_HAS(bgp, TALERT)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1383 | free_irq(bgp->irq, bgp); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1384 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1385 | if (TI_BANDGAP_HAS(bgp, TSHUT)) { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1386 | free_irq(gpio_to_irq(bgp->tshut_gpio), NULL); |
| 1387 | gpio_free(bgp->tshut_gpio); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | return 0; |
| 1391 | } |
| 1392 | |
Grygorii Strashko | 3992b62 | 2015-02-06 16:55:46 +0200 | [diff] [blame] | 1393 | #ifdef CONFIG_PM_SLEEP |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1394 | static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1395 | { |
| 1396 | int i; |
| 1397 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1398 | for (i = 0; i < bgp->conf->sensor_count; i++) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1399 | struct temp_sensor_registers *tsr; |
| 1400 | struct temp_sensor_regval *rval; |
| 1401 | |
Eduardo Valentin | 9879b2c | 2013-03-19 10:54:23 -0400 | [diff] [blame] | 1402 | rval = &bgp->regval[i]; |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1403 | tsr = bgp->conf->sensors[i].registers; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1404 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1405 | if (TI_BANDGAP_HAS(bgp, MODE_CONFIG)) |
| 1406 | rval->bg_mode_ctrl = ti_bandgap_readl(bgp, |
J Keerthy | 76d2cd3 | 2012-09-11 19:06:52 +0300 | [diff] [blame] | 1407 | tsr->bgap_mode_ctrl); |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1408 | if (TI_BANDGAP_HAS(bgp, COUNTER)) |
| 1409 | rval->bg_counter = ti_bandgap_readl(bgp, |
J Keerthy | 76d2cd3 | 2012-09-11 19:06:52 +0300 | [diff] [blame] | 1410 | tsr->bgap_counter); |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1411 | if (TI_BANDGAP_HAS(bgp, TALERT)) { |
| 1412 | rval->bg_threshold = ti_bandgap_readl(bgp, |
J Keerthy | 76d2cd3 | 2012-09-11 19:06:52 +0300 | [diff] [blame] | 1413 | tsr->bgap_threshold); |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1414 | rval->bg_ctrl = ti_bandgap_readl(bgp, |
J Keerthy | 76d2cd3 | 2012-09-11 19:06:52 +0300 | [diff] [blame] | 1415 | tsr->bgap_mask_ctrl); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1416 | } |
| 1417 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1418 | if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) |
| 1419 | rval->tshut_threshold = ti_bandgap_readl(bgp, |
J Keerthy | 76d2cd3 | 2012-09-11 19:06:52 +0300 | [diff] [blame] | 1420 | tsr->tshut_threshold); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | return 0; |
| 1424 | } |
| 1425 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1426 | static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1427 | { |
| 1428 | int i; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1429 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1430 | for (i = 0; i < bgp->conf->sensor_count; i++) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1431 | struct temp_sensor_registers *tsr; |
| 1432 | struct temp_sensor_regval *rval; |
| 1433 | u32 val = 0; |
| 1434 | |
Eduardo Valentin | 9879b2c | 2013-03-19 10:54:23 -0400 | [diff] [blame] | 1435 | rval = &bgp->regval[i]; |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1436 | tsr = bgp->conf->sensors[i].registers; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1437 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1438 | if (TI_BANDGAP_HAS(bgp, COUNTER)) |
| 1439 | val = ti_bandgap_readl(bgp, tsr->bgap_counter); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1440 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1441 | if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) |
| 1442 | ti_bandgap_writel(bgp, rval->tshut_threshold, |
| 1443 | tsr->tshut_threshold); |
Radhesh Fadnis | b87ea75 | 2012-11-13 14:10:04 -0400 | [diff] [blame] | 1444 | /* Force immediate temperature measurement and update |
| 1445 | * of the DTEMP field |
| 1446 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1447 | ti_bandgap_force_single_read(bgp, i); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1448 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1449 | if (TI_BANDGAP_HAS(bgp, COUNTER)) |
| 1450 | ti_bandgap_writel(bgp, rval->bg_counter, |
| 1451 | tsr->bgap_counter); |
| 1452 | if (TI_BANDGAP_HAS(bgp, MODE_CONFIG)) |
| 1453 | ti_bandgap_writel(bgp, rval->bg_mode_ctrl, |
| 1454 | tsr->bgap_mode_ctrl); |
| 1455 | if (TI_BANDGAP_HAS(bgp, TALERT)) { |
| 1456 | ti_bandgap_writel(bgp, rval->bg_threshold, |
| 1457 | tsr->bgap_threshold); |
| 1458 | ti_bandgap_writel(bgp, rval->bg_ctrl, |
| 1459 | tsr->bgap_mask_ctrl); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | return 0; |
| 1464 | } |
| 1465 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1466 | static int ti_bandgap_suspend(struct device *dev) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1467 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1468 | struct ti_bandgap *bgp = dev_get_drvdata(dev); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1469 | int err; |
| 1470 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1471 | err = ti_bandgap_save_ctxt(bgp); |
| 1472 | ti_bandgap_power(bgp, false); |
Radhesh Fadnis | 6c9c1d6 | 2013-02-26 18:53:25 -0400 | [diff] [blame] | 1473 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1474 | if (TI_BANDGAP_HAS(bgp, CLK_CTRL)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1475 | clk_disable_unprepare(bgp->fclock); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1476 | |
| 1477 | return err; |
| 1478 | } |
| 1479 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1480 | static int ti_bandgap_resume(struct device *dev) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1481 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1482 | struct ti_bandgap *bgp = dev_get_drvdata(dev); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1483 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1484 | if (TI_BANDGAP_HAS(bgp, CLK_CTRL)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1485 | clk_prepare_enable(bgp->fclock); |
Radhesh Fadnis | 6c9c1d6 | 2013-02-26 18:53:25 -0400 | [diff] [blame] | 1486 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1487 | ti_bandgap_power(bgp, true); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1488 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1489 | return ti_bandgap_restore_ctxt(bgp); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1490 | } |
Jingoo Han | 5204f8c | 2014-02-27 20:43:02 +0900 | [diff] [blame] | 1491 | static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend, |
| 1492 | ti_bandgap_resume); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1493 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1494 | #define DEV_PM_OPS (&ti_bandgap_dev_pm_ops) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1495 | #else |
| 1496 | #define DEV_PM_OPS NULL |
| 1497 | #endif |
| 1498 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1499 | static const struct of_device_id of_ti_bandgap_match[] = { |
Eduardo Valentin | 1a31270 | 2012-07-12 19:02:31 +0300 | [diff] [blame] | 1500 | #ifdef CONFIG_OMAP4_THERMAL |
| 1501 | { |
| 1502 | .compatible = "ti,omap4430-bandgap", |
| 1503 | .data = (void *)&omap4430_data, |
| 1504 | }, |
| 1505 | { |
| 1506 | .compatible = "ti,omap4460-bandgap", |
| 1507 | .data = (void *)&omap4460_data, |
| 1508 | }, |
| 1509 | { |
| 1510 | .compatible = "ti,omap4470-bandgap", |
| 1511 | .data = (void *)&omap4470_data, |
| 1512 | }, |
| 1513 | #endif |
Eduardo Valentin | 949f5a5 | 2012-07-12 19:02:32 +0300 | [diff] [blame] | 1514 | #ifdef CONFIG_OMAP5_THERMAL |
| 1515 | { |
| 1516 | .compatible = "ti,omap5430-bandgap", |
| 1517 | .data = (void *)&omap5430_data, |
| 1518 | }, |
| 1519 | #endif |
Eduardo Valentin | 25870e6 | 2013-05-29 15:07:45 +0000 | [diff] [blame] | 1520 | #ifdef CONFIG_DRA752_THERMAL |
| 1521 | { |
| 1522 | .compatible = "ti,dra752-bandgap", |
| 1523 | .data = (void *)&dra752_data, |
| 1524 | }, |
| 1525 | #endif |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1526 | /* Sentinel */ |
| 1527 | { }, |
| 1528 | }; |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1529 | MODULE_DEVICE_TABLE(of, of_ti_bandgap_match); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1530 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1531 | static struct platform_driver ti_bandgap_sensor_driver = { |
| 1532 | .probe = ti_bandgap_probe, |
| 1533 | .remove = ti_bandgap_remove, |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1534 | .driver = { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1535 | .name = "ti-soc-thermal", |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1536 | .pm = DEV_PM_OPS, |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1537 | .of_match_table = of_ti_bandgap_match, |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1538 | }, |
| 1539 | }; |
| 1540 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1541 | module_platform_driver(ti_bandgap_sensor_driver); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1542 | |
| 1543 | MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver"); |
| 1544 | MODULE_LICENSE("GPL v2"); |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1545 | MODULE_ALIAS("platform:ti-soc-thermal"); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1546 | MODULE_AUTHOR("Texas Instrument Inc."); |