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 | |
Pavel Machek | 95d079e | 2015-03-24 23:20:21 +0100 | [diff] [blame] | 46 | static int ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id); |
| 47 | |
Eduardo Valentin | 8abbe71 | 2013-03-15 09:00:05 -0400 | [diff] [blame] | 48 | /*** Helper functions to access registers and their bitfields ***/ |
| 49 | |
Eduardo Valentin | 9c468aa | 2013-03-15 08:59:56 -0400 | [diff] [blame] | 50 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 51 | * ti_bandgap_readl() - simple read helper function |
| 52 | * @bgp: pointer to ti_bandgap structure |
Eduardo Valentin | 9c468aa | 2013-03-15 08:59:56 -0400 | [diff] [blame] | 53 | * @reg: desired register (offset) to be read |
| 54 | * |
| 55 | * Helper function to read bandgap registers. It uses the io remapped area. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 56 | * Return: the register value. |
Eduardo Valentin | 9c468aa | 2013-03-15 08:59:56 -0400 | [diff] [blame] | 57 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 58 | static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 59 | { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 60 | return readl(bgp->base + reg); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 61 | } |
| 62 | |
Eduardo Valentin | 9c468aa | 2013-03-15 08:59:56 -0400 | [diff] [blame] | 63 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 64 | * ti_bandgap_writel() - simple write helper function |
| 65 | * @bgp: pointer to ti_bandgap structure |
Eduardo Valentin | 9c468aa | 2013-03-15 08:59:56 -0400 | [diff] [blame] | 66 | * @val: desired register value to be written |
| 67 | * @reg: desired register (offset) to be written |
| 68 | * |
| 69 | * Helper function to write bandgap registers. It uses the io remapped area. |
| 70 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 71 | 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] | 72 | { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 73 | writel(val, bgp->base + reg); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 74 | } |
| 75 | |
Eduardo Valentin | 9c468aa | 2013-03-15 08:59:56 -0400 | [diff] [blame] | 76 | /** |
| 77 | * DOC: macro to update bits. |
| 78 | * |
| 79 | * RMW_BITS() - used to read, modify and update bandgap bitfields. |
| 80 | * The value passed will be shifted. |
| 81 | */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 82 | #define RMW_BITS(bgp, id, reg, mask, val) \ |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 83 | do { \ |
| 84 | struct temp_sensor_registers *t; \ |
| 85 | u32 r; \ |
| 86 | \ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 87 | t = bgp->conf->sensors[(id)].registers; \ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 88 | r = ti_bandgap_readl(bgp, t->reg); \ |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 89 | r &= ~t->mask; \ |
| 90 | r |= (val) << __ffs(t->mask); \ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 91 | ti_bandgap_writel(bgp, r, t->reg); \ |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 92 | } while (0) |
| 93 | |
Eduardo Valentin | 6ab5240 | 2013-03-15 09:00:06 -0400 | [diff] [blame] | 94 | /*** Basic helper functions ***/ |
| 95 | |
Eduardo Valentin | 7a556e6 | 2013-03-15 08:59:58 -0400 | [diff] [blame] | 96 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 97 | * ti_bandgap_power() - controls the power state of a bandgap device |
| 98 | * @bgp: pointer to ti_bandgap structure |
Eduardo Valentin | 7a556e6 | 2013-03-15 08:59:58 -0400 | [diff] [blame] | 99 | * @on: desired power state (1 - on, 0 - off) |
| 100 | * |
| 101 | * Used to power on/off a bandgap device instance. Only used on those |
| 102 | * that features tempsoff bit. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 103 | * |
| 104 | * Return: 0 on success, -ENOTSUPP if tempsoff is not supported. |
Eduardo Valentin | 7a556e6 | 2013-03-15 08:59:58 -0400 | [diff] [blame] | 105 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 106 | static int ti_bandgap_power(struct ti_bandgap *bgp, bool on) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 107 | { |
Pavel Machek | e34238bf | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 108 | int i; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 109 | |
Pavel Machek | e34238bf | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 110 | if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH)) |
| 111 | return -ENOTSUPP; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 112 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 113 | for (i = 0; i < bgp->conf->sensor_count; i++) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 114 | /* active on 0 */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 115 | RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on); |
Pavel Machek | e34238bf | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 116 | return 0; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 117 | } |
| 118 | |
Eduardo Valentin | 4a6554e | 2013-03-15 08:59:59 -0400 | [diff] [blame] | 119 | /** |
Keerthy | 7901063 | 2015-04-22 18:21:41 +0530 | [diff] [blame] | 120 | * ti_errata814_bandgap_read_temp() - helper function to read dra7 sensor temperature |
| 121 | * @bgp: pointer to ti_bandgap structure |
| 122 | * @reg: desired register (offset) to be read |
| 123 | * |
| 124 | * Function to read dra7 bandgap sensor temperature. This is done separately |
| 125 | * so as to workaround the errata "Bandgap Temperature read Dtemp can be |
| 126 | * corrupted" - Errata ID: i814". |
| 127 | * Read accesses to registers listed below can be corrupted due to incorrect |
| 128 | * resynchronization between clock domains. |
| 129 | * Read access to registers below can be corrupted : |
| 130 | * CTRL_CORE_DTEMP_MPU/GPU/CORE/DSPEVE/IVA_n (n = 0 to 4) |
| 131 | * CTRL_CORE_TEMP_SENSOR_MPU/GPU/CORE/DSPEVE/IVA_n |
| 132 | * |
| 133 | * Return: the register value. |
| 134 | */ |
| 135 | static u32 ti_errata814_bandgap_read_temp(struct ti_bandgap *bgp, u32 reg) |
| 136 | { |
| 137 | u32 val1, val2; |
| 138 | |
| 139 | val1 = ti_bandgap_readl(bgp, reg); |
| 140 | val2 = ti_bandgap_readl(bgp, reg); |
| 141 | |
| 142 | /* If both times we read the same value then that is right */ |
| 143 | if (val1 == val2) |
| 144 | return val1; |
| 145 | |
| 146 | /* if val1 and val2 are different read it third time */ |
| 147 | return ti_bandgap_readl(bgp, reg); |
| 148 | } |
| 149 | |
| 150 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 151 | * ti_bandgap_read_temp() - helper function to read sensor temperature |
| 152 | * @bgp: pointer to ti_bandgap structure |
Eduardo Valentin | 4a6554e | 2013-03-15 08:59:59 -0400 | [diff] [blame] | 153 | * @id: bandgap sensor id |
| 154 | * |
| 155 | * Function to concentrate the steps to read sensor temperature register. |
| 156 | * This function is desired because, depending on bandgap device version, |
| 157 | * it might be needed to freeze the bandgap state machine, before fetching |
| 158 | * the register value. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 159 | * |
| 160 | * Return: temperature in ADC values. |
Eduardo Valentin | 4a6554e | 2013-03-15 08:59:59 -0400 | [diff] [blame] | 161 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 162 | static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id) |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 163 | { |
| 164 | struct temp_sensor_registers *tsr; |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 165 | u32 temp, reg; |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 166 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 167 | tsr = bgp->conf->sensors[id].registers; |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 168 | reg = tsr->temp_sensor_ctrl; |
| 169 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 170 | if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 171 | RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1); |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 172 | /* |
| 173 | * In case we cannot read from cur_dtemp / dtemp_0, |
| 174 | * then we read from the last valid temp read |
| 175 | */ |
| 176 | reg = tsr->ctrl_dtemp_1; |
| 177 | } |
| 178 | |
| 179 | /* read temperature */ |
Keerthy | 7901063 | 2015-04-22 18:21:41 +0530 | [diff] [blame] | 180 | if (TI_BANDGAP_HAS(bgp, ERRATA_814)) |
| 181 | temp = ti_errata814_bandgap_read_temp(bgp, reg); |
| 182 | else |
| 183 | temp = ti_bandgap_readl(bgp, reg); |
| 184 | |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 185 | temp &= tsr->bgap_dtemp_mask; |
| 186 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 187 | if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 188 | RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0); |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 189 | |
| 190 | return temp; |
| 191 | } |
| 192 | |
Eduardo Valentin | fb65b88 | 2013-03-15 09:00:07 -0400 | [diff] [blame] | 193 | /*** IRQ handlers ***/ |
| 194 | |
Eduardo Valentin | ee07d55 | 2013-03-15 09:00:01 -0400 | [diff] [blame] | 195 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 196 | * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs |
Eduardo Valentin | ee07d55 | 2013-03-15 09:00:01 -0400 | [diff] [blame] | 197 | * @irq: IRQ number |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 198 | * @data: private data (struct ti_bandgap *) |
Eduardo Valentin | ee07d55 | 2013-03-15 09:00:01 -0400 | [diff] [blame] | 199 | * |
| 200 | * This is the Talert handler. Use it only if bandgap device features |
| 201 | * HAS(TALERT). This handler goes over all sensors and checks their |
| 202 | * conditions and acts accordingly. In case there are events pending, |
| 203 | * it will reset the event mask to wait for the opposite event (next event). |
| 204 | * 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] | 205 | * |
| 206 | * Return: IRQ_HANDLED |
Eduardo Valentin | ee07d55 | 2013-03-15 09:00:01 -0400 | [diff] [blame] | 207 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 208 | static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 209 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 210 | struct ti_bandgap *bgp = data; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 211 | struct temp_sensor_registers *tsr; |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 212 | u32 t_hot = 0, t_cold = 0, ctrl; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 213 | int i; |
| 214 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 215 | spin_lock(&bgp->lock); |
| 216 | for (i = 0; i < bgp->conf->sensor_count; i++) { |
| 217 | tsr = bgp->conf->sensors[i].registers; |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 218 | ctrl = ti_bandgap_readl(bgp, tsr->bgap_status); |
Eduardo Valentin | e555c95 | 2013-03-15 09:00:04 -0400 | [diff] [blame] | 219 | |
| 220 | /* Read the status of t_hot */ |
| 221 | t_hot = ctrl & tsr->status_hot_mask; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 222 | |
| 223 | /* Read the status of t_cold */ |
Eduardo Valentin | e555c95 | 2013-03-15 09:00:04 -0400 | [diff] [blame] | 224 | t_cold = ctrl & tsr->status_cold_mask; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 225 | |
| 226 | if (!t_cold && !t_hot) |
| 227 | continue; |
| 228 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 229 | ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 230 | /* |
| 231 | * One TALERT interrupt: Two sources |
| 232 | * If the interrupt is due to t_hot then mask t_hot and |
| 233 | * and unmask t_cold else mask t_cold and unmask t_hot |
| 234 | */ |
| 235 | if (t_hot) { |
| 236 | ctrl &= ~tsr->mask_hot_mask; |
| 237 | ctrl |= tsr->mask_cold_mask; |
| 238 | } else if (t_cold) { |
| 239 | ctrl &= ~tsr->mask_cold_mask; |
| 240 | ctrl |= tsr->mask_hot_mask; |
| 241 | } |
| 242 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 243 | ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 244 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 245 | dev_dbg(bgp->dev, |
Eduardo Valentin | 71e303f | 2012-11-13 14:10:03 -0400 | [diff] [blame] | 246 | "%s: IRQ from %s sensor: hotevent %d coldevent %d\n", |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 247 | __func__, bgp->conf->sensors[i].domain, |
Eduardo Valentin | 71e303f | 2012-11-13 14:10:03 -0400 | [diff] [blame] | 248 | t_hot, t_cold); |
| 249 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 250 | /* report temperature to whom may concern */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 251 | if (bgp->conf->report_temperature) |
| 252 | bgp->conf->report_temperature(bgp, i); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 253 | } |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 254 | spin_unlock(&bgp->lock); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 255 | |
| 256 | return IRQ_HANDLED; |
| 257 | } |
| 258 | |
Eduardo Valentin | 79857cd2 | 2013-03-15 09:00:02 -0400 | [diff] [blame] | 259 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 260 | * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal |
Eduardo Valentin | 79857cd2 | 2013-03-15 09:00:02 -0400 | [diff] [blame] | 261 | * @irq: IRQ number |
| 262 | * @data: private data (unused) |
| 263 | * |
| 264 | * This is the Tshut handler. Use it only if bandgap device features |
| 265 | * HAS(TSHUT). If any sensor fires the Tshut signal, we simply shutdown |
| 266 | * the system. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 267 | * |
| 268 | * Return: IRQ_HANDLED |
Eduardo Valentin | 79857cd2 | 2013-03-15 09:00:02 -0400 | [diff] [blame] | 269 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 270 | static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 271 | { |
Ruslan Ruslichenko | b3bf0e9 | 2013-02-26 18:53:24 -0400 | [diff] [blame] | 272 | pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n", |
| 273 | __func__); |
| 274 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 275 | orderly_poweroff(true); |
| 276 | |
| 277 | return IRQ_HANDLED; |
| 278 | } |
| 279 | |
Eduardo Valentin | 2f6af4b | 2013-03-15 09:00:08 -0400 | [diff] [blame] | 280 | /*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/ |
| 281 | |
Eduardo Valentin | 2577e93 | 2013-03-15 09:00:11 -0400 | [diff] [blame] | 282 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 283 | * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale |
| 284 | * @bgp: struct ti_bandgap pointer |
Eduardo Valentin | 2577e93 | 2013-03-15 09:00:11 -0400 | [diff] [blame] | 285 | * @adc_val: value in ADC representation |
| 286 | * @t: address where to write the resulting temperature in mCelsius |
| 287 | * |
| 288 | * Simple conversion from ADC representation to mCelsius. In case the ADC value |
| 289 | * is out of the ADC conv table range, it returns -ERANGE, 0 on success. |
| 290 | * The conversion table is indexed by the ADC values. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 291 | * |
| 292 | * Return: 0 if conversion was successful, else -ERANGE in case the @adc_val |
| 293 | * argument is out of the ADC conv table range. |
Eduardo Valentin | 2577e93 | 2013-03-15 09:00:11 -0400 | [diff] [blame] | 294 | */ |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 295 | static |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 296 | 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] | 297 | { |
Eduardo Valentin | 9879b2c | 2013-03-19 10:54:23 -0400 | [diff] [blame] | 298 | const struct ti_bandgap_data *conf = bgp->conf; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 299 | |
| 300 | /* look up for temperature in the table and return the temperature */ |
Pavel Machek | e34238bf | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 301 | if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val) |
| 302 | return -ERANGE; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 303 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 304 | *t = bgp->conf->conv_table[adc_val - conf->adc_start_val]; |
Pavel Machek | e34238bf | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 305 | return 0; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 306 | } |
| 307 | |
Eduardo Valentin | e7f60b5 | 2013-03-15 09:00:15 -0400 | [diff] [blame] | 308 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 309 | * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap |
| 310 | * @bgp: struct ti_bandgap pointer |
Eduardo Valentin | e72b7bb | 2013-03-15 09:00:38 -0400 | [diff] [blame] | 311 | * @id: bandgap sensor id |
| 312 | * |
| 313 | * Checks if the bandgap pointer is valid and if the sensor id is also |
| 314 | * applicable. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 315 | * |
| 316 | * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if |
| 317 | * @id cannot index @bgp sensors. |
Eduardo Valentin | e72b7bb | 2013-03-15 09:00:38 -0400 | [diff] [blame] | 318 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 319 | static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 320 | { |
Eduardo Valentin | 0c12b5a | 2013-05-29 15:07:43 +0000 | [diff] [blame] | 321 | if (!bgp || IS_ERR(bgp)) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 322 | pr_err("%s: invalid bandgap pointer\n", __func__); |
Pavel Machek | e34238bf | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 323 | return -EINVAL; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 324 | } |
| 325 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 326 | if ((id < 0) || (id >= bgp->conf->sensor_count)) { |
| 327 | dev_err(bgp->dev, "%s: sensor id out of range (%d)\n", |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 328 | __func__, id); |
Pavel Machek | e34238bf | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 329 | return -ERANGE; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 330 | } |
| 331 | |
Pavel Machek | e34238bf | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 332 | return 0; |
Eduardo Valentin | 56f132f | 2013-03-15 09:00:21 -0400 | [diff] [blame] | 333 | } |
| 334 | |
Eduardo Valentin | 9efa93b | 2013-03-15 09:00:28 -0400 | [diff] [blame] | 335 | /** |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 336 | * ti_bandgap_read_counter() - read the sensor counter |
| 337 | * @bgp: pointer to bandgap instance |
| 338 | * @id: sensor id |
| 339 | * @interval: resulting update interval in miliseconds |
| 340 | */ |
| 341 | static void ti_bandgap_read_counter(struct ti_bandgap *bgp, int id, |
| 342 | int *interval) |
| 343 | { |
| 344 | struct temp_sensor_registers *tsr; |
| 345 | int time; |
| 346 | |
| 347 | tsr = bgp->conf->sensors[id].registers; |
| 348 | time = ti_bandgap_readl(bgp, tsr->bgap_counter); |
| 349 | time = (time & tsr->counter_mask) >> |
| 350 | __ffs(tsr->counter_mask); |
| 351 | time = time * 1000 / bgp->clk_rate; |
| 352 | *interval = time; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * ti_bandgap_read_counter_delay() - read the sensor counter delay |
| 357 | * @bgp: pointer to bandgap instance |
| 358 | * @id: sensor id |
| 359 | * @interval: resulting update interval in miliseconds |
| 360 | */ |
| 361 | static void ti_bandgap_read_counter_delay(struct ti_bandgap *bgp, int id, |
| 362 | int *interval) |
| 363 | { |
| 364 | struct temp_sensor_registers *tsr; |
| 365 | int reg_val; |
| 366 | |
| 367 | tsr = bgp->conf->sensors[id].registers; |
| 368 | |
| 369 | reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl); |
| 370 | reg_val = (reg_val & tsr->mask_counter_delay_mask) >> |
| 371 | __ffs(tsr->mask_counter_delay_mask); |
| 372 | switch (reg_val) { |
| 373 | case 0: |
| 374 | *interval = 0; |
| 375 | break; |
| 376 | case 1: |
| 377 | *interval = 1; |
| 378 | break; |
| 379 | case 2: |
| 380 | *interval = 10; |
| 381 | break; |
| 382 | case 3: |
| 383 | *interval = 100; |
| 384 | break; |
| 385 | case 4: |
| 386 | *interval = 250; |
| 387 | break; |
| 388 | case 5: |
| 389 | *interval = 500; |
| 390 | break; |
| 391 | default: |
| 392 | dev_warn(bgp->dev, "Wrong counter delay value read from register %X", |
| 393 | reg_val); |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 398 | * ti_bandgap_read_update_interval() - read the sensor update interval |
Eduardo Valentin | 61603af | 2013-03-19 10:54:25 -0400 | [diff] [blame] | 399 | * @bgp: pointer to bandgap instance |
| 400 | * @id: sensor id |
| 401 | * @interval: resulting update interval in miliseconds |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 402 | * |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 403 | * Return: 0 on success or the proper error code |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 404 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 405 | int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id, |
| 406 | int *interval) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 407 | { |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 408 | int ret = 0; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 409 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 410 | ret = ti_bandgap_validate(bgp, id); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 411 | if (ret) |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 412 | goto exit; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 413 | |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 414 | if (!TI_BANDGAP_HAS(bgp, COUNTER) && |
| 415 | !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) { |
| 416 | ret = -ENOTSUPP; |
| 417 | goto exit; |
| 418 | } |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 419 | |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 420 | if (TI_BANDGAP_HAS(bgp, COUNTER)) { |
| 421 | ti_bandgap_read_counter(bgp, id, interval); |
| 422 | goto exit; |
| 423 | } |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 424 | |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 425 | ti_bandgap_read_counter_delay(bgp, id, interval); |
| 426 | exit: |
| 427 | return ret; |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * ti_bandgap_write_counter_delay() - set the counter_delay |
| 432 | * @bgp: pointer to bandgap instance |
| 433 | * @id: sensor id |
| 434 | * @interval: desired update interval in miliseconds |
| 435 | * |
| 436 | * Return: 0 on success or the proper error code |
| 437 | */ |
| 438 | static int ti_bandgap_write_counter_delay(struct ti_bandgap *bgp, int id, |
| 439 | u32 interval) |
| 440 | { |
| 441 | int rval; |
| 442 | |
| 443 | switch (interval) { |
| 444 | case 0: /* Immediate conversion */ |
| 445 | rval = 0x0; |
| 446 | break; |
| 447 | case 1: /* Conversion after ever 1ms */ |
| 448 | rval = 0x1; |
| 449 | break; |
| 450 | case 10: /* Conversion after ever 10ms */ |
| 451 | rval = 0x2; |
| 452 | break; |
| 453 | case 100: /* Conversion after ever 100ms */ |
| 454 | rval = 0x3; |
| 455 | break; |
| 456 | case 250: /* Conversion after ever 250ms */ |
| 457 | rval = 0x4; |
| 458 | break; |
| 459 | case 500: /* Conversion after ever 500ms */ |
| 460 | rval = 0x5; |
| 461 | break; |
| 462 | default: |
| 463 | dev_warn(bgp->dev, "Delay %d ms is not supported\n", interval); |
| 464 | return -EINVAL; |
| 465 | } |
| 466 | |
| 467 | spin_lock(&bgp->lock); |
| 468 | RMW_BITS(bgp, id, bgap_mask_ctrl, mask_counter_delay_mask, rval); |
| 469 | spin_unlock(&bgp->lock); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | /** |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 475 | * ti_bandgap_write_counter() - set the bandgap sensor counter |
| 476 | * @bgp: pointer to bandgap instance |
| 477 | * @id: sensor id |
| 478 | * @interval: desired update interval in miliseconds |
| 479 | */ |
| 480 | static void ti_bandgap_write_counter(struct ti_bandgap *bgp, int id, |
| 481 | u32 interval) |
| 482 | { |
| 483 | interval = interval * bgp->clk_rate / 1000; |
| 484 | spin_lock(&bgp->lock); |
| 485 | RMW_BITS(bgp, id, bgap_counter, counter_mask, interval); |
| 486 | spin_unlock(&bgp->lock); |
| 487 | } |
| 488 | |
| 489 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 490 | * ti_bandgap_write_update_interval() - set the update interval |
Eduardo Valentin | 61603af | 2013-03-19 10:54:25 -0400 | [diff] [blame] | 491 | * @bgp: pointer to bandgap instance |
| 492 | * @id: sensor id |
| 493 | * @interval: desired update interval in miliseconds |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 494 | * |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 495 | * Return: 0 on success or the proper error code |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 496 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 497 | int ti_bandgap_write_update_interval(struct ti_bandgap *bgp, |
| 498 | int id, u32 interval) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 499 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 500 | int ret = ti_bandgap_validate(bgp, id); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 501 | if (ret) |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 502 | goto exit; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 503 | |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 504 | if (!TI_BANDGAP_HAS(bgp, COUNTER) && |
| 505 | !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) { |
| 506 | ret = -ENOTSUPP; |
| 507 | goto exit; |
| 508 | } |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 509 | |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 510 | if (TI_BANDGAP_HAS(bgp, COUNTER)) { |
| 511 | ti_bandgap_write_counter(bgp, id, interval); |
| 512 | goto exit; |
| 513 | } |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 514 | |
J Keerthy | 58bccd0 | 2013-04-01 12:04:42 -0400 | [diff] [blame] | 515 | ret = ti_bandgap_write_counter_delay(bgp, id, interval); |
| 516 | exit: |
| 517 | return ret; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 521 | * ti_bandgap_read_temperature() - report current temperature |
Eduardo Valentin | 61603af | 2013-03-19 10:54:25 -0400 | [diff] [blame] | 522 | * @bgp: pointer to bandgap instance |
| 523 | * @id: sensor id |
| 524 | * @temperature: resulting temperature |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 525 | * |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 526 | * Return: 0 on success or the proper error code |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 527 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 528 | int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id, |
| 529 | int *temperature) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 530 | { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 531 | u32 temp; |
| 532 | int ret; |
| 533 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 534 | ret = ti_bandgap_validate(bgp, id); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 535 | if (ret) |
| 536 | return ret; |
| 537 | |
Pavel Machek | 95d079e | 2015-03-24 23:20:21 +0100 | [diff] [blame] | 538 | if (!TI_BANDGAP_HAS(bgp, MODE_CONFIG)) { |
| 539 | ret = ti_bandgap_force_single_read(bgp, id); |
| 540 | if (ret) |
| 541 | return ret; |
| 542 | } |
| 543 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 544 | spin_lock(&bgp->lock); |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 545 | temp = ti_bandgap_read_temp(bgp, id); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 546 | spin_unlock(&bgp->lock); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 547 | |
Pavel Machek | e34238bf | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 548 | ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 549 | if (ret) |
| 550 | return -EIO; |
| 551 | |
| 552 | *temperature = temp; |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 558 | * ti_bandgap_set_sensor_data() - helper function to store thermal |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 559 | * framework related data. |
Eduardo Valentin | 61603af | 2013-03-19 10:54:25 -0400 | [diff] [blame] | 560 | * @bgp: pointer to bandgap instance |
| 561 | * @id: sensor id |
| 562 | * @data: thermal framework related data to be stored |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 563 | * |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 564 | * Return: 0 on success or the proper error code |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 565 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 566 | 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] | 567 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 568 | int ret = ti_bandgap_validate(bgp, id); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 569 | if (ret) |
| 570 | return ret; |
| 571 | |
Eduardo Valentin | 9879b2c | 2013-03-19 10:54:23 -0400 | [diff] [blame] | 572 | bgp->regval[id].data = data; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 573 | |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 578 | * ti_bandgap_get_sensor_data() - helper function to get thermal |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 579 | * framework related data. |
Eduardo Valentin | 61603af | 2013-03-19 10:54:25 -0400 | [diff] [blame] | 580 | * @bgp: pointer to bandgap instance |
| 581 | * @id: sensor id |
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: data stored by set function with sensor id on success or NULL |
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 | void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id) |
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 | int ret = ti_bandgap_validate(bgp, id); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 588 | if (ret) |
| 589 | return ERR_PTR(ret); |
| 590 | |
Eduardo Valentin | 9879b2c | 2013-03-19 10:54:23 -0400 | [diff] [blame] | 591 | return bgp->regval[id].data; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 592 | } |
| 593 | |
Eduardo Valentin | e195aba | 2013-03-15 09:00:22 -0400 | [diff] [blame] | 594 | /*** Helper functions used during device initialization ***/ |
| 595 | |
Eduardo Valentin | 31102a7 | 2013-03-15 09:00:26 -0400 | [diff] [blame] | 596 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 597 | * ti_bandgap_force_single_read() - executes 1 single ADC conversion |
| 598 | * @bgp: pointer to struct ti_bandgap |
Eduardo Valentin | 31102a7 | 2013-03-15 09:00:26 -0400 | [diff] [blame] | 599 | * @id: sensor id which it is desired to read 1 temperature |
| 600 | * |
| 601 | * Used to initialize the conversion state machine and set it to a valid |
| 602 | * state. Called during device initialization and context restore events. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 603 | * |
| 604 | * Return: 0 |
Eduardo Valentin | 31102a7 | 2013-03-15 09:00:26 -0400 | [diff] [blame] | 605 | */ |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 606 | static int |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 607 | ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 608 | { |
Pavel Machek | a4296d1 | 2015-01-18 21:20:51 +0100 | [diff] [blame] | 609 | u32 counter = 1000; |
| 610 | struct temp_sensor_registers *tsr; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 611 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 612 | /* Select single conversion mode */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 613 | if (TI_BANDGAP_HAS(bgp, MODE_CONFIG)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 614 | RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 615 | |
| 616 | /* Start of Conversion = 1 */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 617 | RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1); |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 618 | |
Pavel Machek | a4296d1 | 2015-01-18 21:20:51 +0100 | [diff] [blame] | 619 | /* Wait for EOCZ going up */ |
| 620 | tsr = bgp->conf->sensors[id].registers; |
| 621 | |
| 622 | while (--counter) { |
| 623 | if (ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) & |
| 624 | tsr->bgap_eocz_mask) |
| 625 | break; |
| 626 | } |
Eduardo Valentin | 194a54f | 2013-02-26 18:53:33 -0400 | [diff] [blame] | 627 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 628 | /* Start of Conversion = 0 */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 629 | RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 630 | |
Pavel Machek | a4296d1 | 2015-01-18 21:20:51 +0100 | [diff] [blame] | 631 | /* Wait for EOCZ going down */ |
| 632 | counter = 1000; |
| 633 | while (--counter) { |
| 634 | if (!(ti_bandgap_readl(bgp, tsr->temp_sensor_ctrl) & |
| 635 | tsr->bgap_eocz_mask)) |
| 636 | break; |
| 637 | } |
| 638 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 639 | return 0; |
| 640 | } |
| 641 | |
| 642 | /** |
Markus Elfring | 8b8656d6 | 2017-04-26 17:11:28 +0200 | [diff] [blame] | 643 | * ti_bandgap_set_continuous_mode() - One time enabling of continuous mode |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 644 | * @bgp: pointer to struct ti_bandgap |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 645 | * |
Eduardo Valentin | a84b6f4 | 2013-03-15 09:00:25 -0400 | [diff] [blame] | 646 | * Call this function only if HAS(MODE_CONFIG) is set. As this driver may |
| 647 | * be used for junction temperature monitoring, it is desirable that the |
| 648 | * sensors are operational all the time, so that alerts are generated |
| 649 | * properly. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 650 | * |
| 651 | * Return: 0 |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 652 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 653 | static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 654 | { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 655 | int i; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 656 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 657 | for (i = 0; i < bgp->conf->sensor_count; i++) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 658 | /* Perform a single read just before enabling continuous */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 659 | ti_bandgap_force_single_read(bgp, i); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 660 | RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
Eduardo Valentin | d3790b3 | 2013-03-15 09:00:30 -0400 | [diff] [blame] | 666 | /** |
J Keerthy | 2f440b0 | 2013-04-01 12:04:45 -0400 | [diff] [blame] | 667 | * ti_bandgap_get_trend() - To fetch the temperature trend of a sensor |
| 668 | * @bgp: pointer to struct ti_bandgap |
| 669 | * @id: id of the individual sensor |
| 670 | * @trend: Pointer to trend. |
| 671 | * |
| 672 | * This function needs to be called to fetch the temperature trend of a |
| 673 | * Particular sensor. The function computes the difference in temperature |
| 674 | * w.r.t time. For the bandgaps with built in history buffer the temperatures |
| 675 | * are read from the buffer and for those without the Buffer -ENOTSUPP is |
| 676 | * returned. |
| 677 | * |
| 678 | * Return: 0 if no error, else return corresponding error. If no |
| 679 | * error then the trend value is passed on to trend parameter |
| 680 | */ |
| 681 | int ti_bandgap_get_trend(struct ti_bandgap *bgp, int id, int *trend) |
| 682 | { |
| 683 | struct temp_sensor_registers *tsr; |
| 684 | u32 temp1, temp2, reg1, reg2; |
| 685 | int t1, t2, interval, ret = 0; |
| 686 | |
| 687 | ret = ti_bandgap_validate(bgp, id); |
| 688 | if (ret) |
| 689 | goto exit; |
| 690 | |
| 691 | if (!TI_BANDGAP_HAS(bgp, HISTORY_BUFFER) || |
| 692 | !TI_BANDGAP_HAS(bgp, FREEZE_BIT)) { |
| 693 | ret = -ENOTSUPP; |
| 694 | goto exit; |
| 695 | } |
| 696 | |
Eduardo Valentin | ba0049e | 2013-06-07 19:13:13 +0000 | [diff] [blame] | 697 | spin_lock(&bgp->lock); |
| 698 | |
J Keerthy | 2f440b0 | 2013-04-01 12:04:45 -0400 | [diff] [blame] | 699 | tsr = bgp->conf->sensors[id].registers; |
| 700 | |
| 701 | /* Freeze and read the last 2 valid readings */ |
Eduardo Valentin | ba0049e | 2013-06-07 19:13:13 +0000 | [diff] [blame] | 702 | RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1); |
J Keerthy | 2f440b0 | 2013-04-01 12:04:45 -0400 | [diff] [blame] | 703 | reg1 = tsr->ctrl_dtemp_1; |
| 704 | reg2 = tsr->ctrl_dtemp_2; |
| 705 | |
| 706 | /* read temperature from history buffer */ |
| 707 | temp1 = ti_bandgap_readl(bgp, reg1); |
| 708 | temp1 &= tsr->bgap_dtemp_mask; |
| 709 | |
| 710 | temp2 = ti_bandgap_readl(bgp, reg2); |
| 711 | temp2 &= tsr->bgap_dtemp_mask; |
| 712 | |
| 713 | /* Convert from adc values to mCelsius temperature */ |
| 714 | ret = ti_bandgap_adc_to_mcelsius(bgp, temp1, &t1); |
| 715 | if (ret) |
Eduardo Valentin | ba0049e | 2013-06-07 19:13:13 +0000 | [diff] [blame] | 716 | goto unfreeze; |
J Keerthy | 2f440b0 | 2013-04-01 12:04:45 -0400 | [diff] [blame] | 717 | |
| 718 | ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2); |
| 719 | if (ret) |
Eduardo Valentin | ba0049e | 2013-06-07 19:13:13 +0000 | [diff] [blame] | 720 | goto unfreeze; |
J Keerthy | 2f440b0 | 2013-04-01 12:04:45 -0400 | [diff] [blame] | 721 | |
| 722 | /* Fetch the update interval */ |
| 723 | ret = ti_bandgap_read_update_interval(bgp, id, &interval); |
Ranganath Krishnan | e838ff8 | 2013-08-23 11:08:23 -0500 | [diff] [blame] | 724 | if (ret) |
Eduardo Valentin | ba0049e | 2013-06-07 19:13:13 +0000 | [diff] [blame] | 725 | goto unfreeze; |
J Keerthy | 2f440b0 | 2013-04-01 12:04:45 -0400 | [diff] [blame] | 726 | |
Ranganath Krishnan | e838ff8 | 2013-08-23 11:08:23 -0500 | [diff] [blame] | 727 | /* Set the interval to 1 ms if bandgap counter delay is not set */ |
| 728 | if (interval == 0) |
| 729 | interval = 1; |
| 730 | |
J Keerthy | 2f440b0 | 2013-04-01 12:04:45 -0400 | [diff] [blame] | 731 | *trend = (t1 - t2) / interval; |
| 732 | |
| 733 | dev_dbg(bgp->dev, "The temperatures are t1 = %d and t2 = %d and trend =%d\n", |
| 734 | t1, t2, *trend); |
| 735 | |
Eduardo Valentin | ba0049e | 2013-06-07 19:13:13 +0000 | [diff] [blame] | 736 | unfreeze: |
| 737 | RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0); |
| 738 | spin_unlock(&bgp->lock); |
J Keerthy | 2f440b0 | 2013-04-01 12:04:45 -0400 | [diff] [blame] | 739 | exit: |
| 740 | return ret; |
| 741 | } |
| 742 | |
| 743 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 744 | * ti_bandgap_tshut_init() - setup and initialize tshut handling |
| 745 | * @bgp: pointer to struct ti_bandgap |
Eduardo Valentin | d3790b3 | 2013-03-15 09:00:30 -0400 | [diff] [blame] | 746 | * @pdev: pointer to device struct platform_device |
| 747 | * |
| 748 | * Call this function only in case the bandgap features HAS(TSHUT). |
| 749 | * In this case, the driver needs to handle the TSHUT signal as an IRQ. |
| 750 | * The IRQ is wired as a GPIO, and for this purpose, it is required |
| 751 | * to specify which GPIO line is used. TSHUT IRQ is fired anytime |
| 752 | * one of the bandgap sensors violates the TSHUT high/hot threshold. |
| 753 | * And in that case, the system must go off. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 754 | * |
| 755 | * Return: 0 if no error, else error status |
Eduardo Valentin | d3790b3 | 2013-03-15 09:00:30 -0400 | [diff] [blame] | 756 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 757 | static int ti_bandgap_tshut_init(struct ti_bandgap *bgp, |
| 758 | struct platform_device *pdev) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 759 | { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 760 | int gpio_nr = bgp->tshut_gpio; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 761 | int status; |
| 762 | |
| 763 | /* Request for gpio_86 line */ |
| 764 | status = gpio_request(gpio_nr, "tshut"); |
| 765 | if (status < 0) { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 766 | 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] | 767 | return status; |
| 768 | } |
| 769 | status = gpio_direction_input(gpio_nr); |
| 770 | if (status) { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 771 | 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] | 772 | return status; |
| 773 | } |
| 774 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 775 | status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler, |
| 776 | IRQF_TRIGGER_RISING, "tshut", NULL); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 777 | if (status) { |
| 778 | gpio_free(gpio_nr); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 779 | dev_err(bgp->dev, "request irq failed for TSHUT"); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | return 0; |
| 783 | } |
| 784 | |
Eduardo Valentin | 094b8ac | 2013-03-15 09:00:31 -0400 | [diff] [blame] | 785 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 786 | * ti_bandgap_alert_init() - setup and initialize talert handling |
| 787 | * @bgp: pointer to struct ti_bandgap |
Eduardo Valentin | 094b8ac | 2013-03-15 09:00:31 -0400 | [diff] [blame] | 788 | * @pdev: pointer to device struct platform_device |
| 789 | * |
| 790 | * Call this function only in case the bandgap features HAS(TALERT). |
| 791 | * In this case, the driver needs to handle the TALERT signals as an IRQs. |
| 792 | * TALERT is a normal IRQ and it is fired any time thresholds (hot or cold) |
| 793 | * are violated. In these situation, the driver must reprogram the thresholds, |
| 794 | * accordingly to specified policy. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 795 | * |
| 796 | * Return: 0 if no error, else return corresponding error. |
Eduardo Valentin | 094b8ac | 2013-03-15 09:00:31 -0400 | [diff] [blame] | 797 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 798 | static int ti_bandgap_talert_init(struct ti_bandgap *bgp, |
| 799 | struct platform_device *pdev) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 800 | { |
| 801 | int ret; |
| 802 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 803 | bgp->irq = platform_get_irq(pdev, 0); |
| 804 | if (bgp->irq < 0) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 805 | dev_err(&pdev->dev, "get_irq failed\n"); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 806 | return bgp->irq; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 807 | } |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 808 | ret = request_threaded_irq(bgp->irq, NULL, |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 809 | ti_bandgap_talert_irq_handler, |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 810 | IRQF_TRIGGER_HIGH | IRQF_ONESHOT, |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 811 | "talert", bgp); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 812 | if (ret) { |
| 813 | dev_err(&pdev->dev, "Request threaded irq failed.\n"); |
| 814 | return ret; |
| 815 | } |
| 816 | |
| 817 | return 0; |
| 818 | } |
| 819 | |
Eduardo Valentin | 61603af | 2013-03-19 10:54:25 -0400 | [diff] [blame] | 820 | static const struct of_device_id of_ti_bandgap_match[]; |
Eduardo Valentin | e9b6f8c | 2013-03-15 09:00:32 -0400 | [diff] [blame] | 821 | /** |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 822 | * ti_bandgap_build() - parse DT and setup a struct ti_bandgap |
Eduardo Valentin | e9b6f8c | 2013-03-15 09:00:32 -0400 | [diff] [blame] | 823 | * @pdev: pointer to device struct platform_device |
| 824 | * |
| 825 | * Used to read the device tree properties accordingly to the bandgap |
| 826 | * matching version. Based on bandgap version and its capabilities it |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 827 | * will build a struct ti_bandgap out of the required DT entries. |
Nishanth Menon | 169e8d0 | 2013-04-01 12:04:34 -0400 | [diff] [blame] | 828 | * |
| 829 | * Return: valid bandgap structure if successful, else returns ERR_PTR |
| 830 | * return value must be verified with IS_ERR. |
Eduardo Valentin | e9b6f8c | 2013-03-15 09:00:32 -0400 | [diff] [blame] | 831 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 832 | static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 833 | { |
| 834 | struct device_node *node = pdev->dev.of_node; |
| 835 | const struct of_device_id *of_id; |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 836 | struct ti_bandgap *bgp; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 837 | struct resource *res; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 838 | int i; |
| 839 | |
| 840 | /* just for the sake */ |
| 841 | if (!node) { |
| 842 | dev_err(&pdev->dev, "no platform information available\n"); |
| 843 | return ERR_PTR(-EINVAL); |
| 844 | } |
| 845 | |
Eduardo Valentin | f684356 | 2013-03-19 10:54:24 -0400 | [diff] [blame] | 846 | bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL); |
Markus Elfring | 57e5211 | 2017-04-26 17:03:07 +0200 | [diff] [blame] | 847 | if (!bgp) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 848 | return ERR_PTR(-ENOMEM); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 849 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 850 | of_id = of_match_device(of_ti_bandgap_match, &pdev->dev); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 851 | if (of_id) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 852 | bgp->conf = of_id->data; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 853 | |
Eduardo Valentin | 9879b2c | 2013-03-19 10:54:23 -0400 | [diff] [blame] | 854 | /* register shadow for context save and restore */ |
Markus Elfring | 748c23d | 2017-04-26 16:45:25 +0200 | [diff] [blame] | 855 | bgp->regval = devm_kcalloc(&pdev->dev, bgp->conf->sensor_count, |
| 856 | sizeof(*bgp->regval), GFP_KERNEL); |
Markus Elfring | 57e5211 | 2017-04-26 17:03:07 +0200 | [diff] [blame] | 857 | if (!bgp->regval) |
Eduardo Valentin | 9879b2c | 2013-03-19 10:54:23 -0400 | [diff] [blame] | 858 | return ERR_PTR(-ENOMEM); |
Eduardo Valentin | 9879b2c | 2013-03-19 10:54:23 -0400 | [diff] [blame] | 859 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 860 | i = 0; |
| 861 | do { |
| 862 | void __iomem *chunk; |
| 863 | |
| 864 | res = platform_get_resource(pdev, IORESOURCE_MEM, i); |
| 865 | if (!res) |
| 866 | break; |
Thierry Reding | 97f4be60 | 2013-01-21 11:09:19 +0100 | [diff] [blame] | 867 | chunk = devm_ioremap_resource(&pdev->dev, res); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 868 | if (i == 0) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 869 | bgp->base = chunk; |
Thierry Reding | 97f4be60 | 2013-01-21 11:09:19 +0100 | [diff] [blame] | 870 | if (IS_ERR(chunk)) |
| 871 | return ERR_CAST(chunk); |
Eduardo Valentin | 24796e1 | 2013-03-15 08:59:53 -0400 | [diff] [blame] | 872 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 873 | i++; |
| 874 | } while (res); |
| 875 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 876 | if (TI_BANDGAP_HAS(bgp, TSHUT)) { |
Eduardo Valentin | 57d1617 | 2013-06-07 11:11:53 -0400 | [diff] [blame] | 877 | bgp->tshut_gpio = of_get_gpio(node, 0); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 878 | if (!gpio_is_valid(bgp->tshut_gpio)) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 879 | dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n", |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 880 | bgp->tshut_gpio); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 881 | return ERR_PTR(-EINVAL); |
| 882 | } |
| 883 | } |
| 884 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 885 | return bgp; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 886 | } |
| 887 | |
Eduardo Valentin | f91ddfe | 2013-03-15 09:00:23 -0400 | [diff] [blame] | 888 | /*** Device driver call backs ***/ |
| 889 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 890 | static |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 891 | int ti_bandgap_probe(struct platform_device *pdev) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 892 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 893 | struct ti_bandgap *bgp; |
Dan Carpenter | 1336919 | 2016-03-02 13:00:55 +0300 | [diff] [blame] | 894 | int clk_rate, ret, i; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 895 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 896 | bgp = ti_bandgap_build(pdev); |
Eduardo Valentin | 0c12b5a | 2013-05-29 15:07:43 +0000 | [diff] [blame] | 897 | if (IS_ERR(bgp)) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 898 | dev_err(&pdev->dev, "failed to fetch platform data\n"); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 899 | return PTR_ERR(bgp); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 900 | } |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 901 | bgp->dev = &pdev->dev; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 902 | |
Pavel Machek | 9c5c87e | 2015-04-02 16:49:07 +0200 | [diff] [blame] | 903 | if (TI_BANDGAP_HAS(bgp, UNRELIABLE)) |
| 904 | dev_warn(&pdev->dev, |
| 905 | "This OMAP thermal sensor is unreliable. You've been warned\n"); |
| 906 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 907 | if (TI_BANDGAP_HAS(bgp, TSHUT)) { |
| 908 | ret = ti_bandgap_tshut_init(bgp, pdev); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 909 | if (ret) { |
| 910 | dev_err(&pdev->dev, |
| 911 | "failed to initialize system tshut IRQ\n"); |
| 912 | return ret; |
| 913 | } |
| 914 | } |
| 915 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 916 | bgp->fclock = clk_get(NULL, bgp->conf->fclock_name); |
Dan Carpenter | 1336919 | 2016-03-02 13:00:55 +0300 | [diff] [blame] | 917 | if (IS_ERR(bgp->fclock)) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 918 | dev_err(&pdev->dev, "failed to request fclock reference\n"); |
Eduardo Valentin | 0c12b5a | 2013-05-29 15:07:43 +0000 | [diff] [blame] | 919 | ret = PTR_ERR(bgp->fclock); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 920 | goto free_irqs; |
| 921 | } |
| 922 | |
Pavel Machek | e34238bf | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 923 | bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name); |
Dan Carpenter | 1336919 | 2016-03-02 13:00:55 +0300 | [diff] [blame] | 924 | if (IS_ERR(bgp->div_clk)) { |
Pavel Machek | e34238bf | 2015-01-18 21:17:10 +0100 | [diff] [blame] | 925 | 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] | 926 | ret = PTR_ERR(bgp->div_clk); |
Luis Henriques | 882f581 | 2016-11-16 22:15:22 +0000 | [diff] [blame] | 927 | goto put_fclock; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 928 | } |
| 929 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 930 | for (i = 0; i < bgp->conf->sensor_count; i++) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 931 | struct temp_sensor_registers *tsr; |
| 932 | u32 val; |
| 933 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 934 | tsr = bgp->conf->sensors[i].registers; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 935 | /* |
| 936 | * check if the efuse has a non-zero value if not |
| 937 | * it is an untrimmed sample and the temperatures |
| 938 | * may not be accurate |
| 939 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 940 | val = ti_bandgap_readl(bgp, tsr->bgap_efuse); |
Dan Carpenter | 1336919 | 2016-03-02 13:00:55 +0300 | [diff] [blame] | 941 | if (!val) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 942 | dev_info(&pdev->dev, |
| 943 | "Non-trimmed BGAP, Temp not accurate\n"); |
| 944 | } |
| 945 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 946 | clk_rate = clk_round_rate(bgp->div_clk, |
| 947 | bgp->conf->sensors[0].ts_data->max_freq); |
| 948 | if (clk_rate < bgp->conf->sensors[0].ts_data->min_freq || |
Paul Walmsley | c68789e | 2013-12-09 18:09:22 -0800 | [diff] [blame] | 949 | clk_rate <= 0) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 950 | ret = -ENODEV; |
| 951 | dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate); |
| 952 | goto put_clks; |
| 953 | } |
| 954 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 955 | ret = clk_set_rate(bgp->div_clk, clk_rate); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 956 | if (ret) |
| 957 | dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n"); |
| 958 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 959 | bgp->clk_rate = clk_rate; |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 960 | if (TI_BANDGAP_HAS(bgp, CLK_CTRL)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 961 | clk_prepare_enable(bgp->fclock); |
Radhesh Fadnis | 6c9c1d6 | 2013-02-26 18:53:25 -0400 | [diff] [blame] | 962 | |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 963 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 964 | spin_lock_init(&bgp->lock); |
| 965 | bgp->dev = &pdev->dev; |
| 966 | platform_set_drvdata(pdev, bgp); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 967 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 968 | ti_bandgap_power(bgp, true); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 969 | |
| 970 | /* Set default counter to 1 for now */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 971 | if (TI_BANDGAP_HAS(bgp, COUNTER)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 972 | for (i = 0; i < bgp->conf->sensor_count; i++) |
| 973 | RMW_BITS(bgp, i, bgap_counter, counter_mask, 1); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 974 | |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 975 | /* Set default thresholds for alert and shutdown */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 976 | for (i = 0; i < bgp->conf->sensor_count; i++) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 977 | struct temp_sensor_data *ts_data; |
| 978 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 979 | ts_data = bgp->conf->sensors[i].ts_data; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 980 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 981 | if (TI_BANDGAP_HAS(bgp, TALERT)) { |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 982 | /* Set initial Talert thresholds */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 983 | RMW_BITS(bgp, i, bgap_threshold, |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 984 | threshold_tcold_mask, ts_data->t_cold); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 985 | RMW_BITS(bgp, i, bgap_threshold, |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 986 | threshold_thot_mask, ts_data->t_hot); |
| 987 | /* Enable the alert events */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 988 | RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1); |
| 989 | RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1); |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 990 | } |
| 991 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 992 | if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) { |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 993 | /* Set initial Tshut thresholds */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 994 | RMW_BITS(bgp, i, tshut_threshold, |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 995 | tshut_hot_mask, ts_data->tshut_hot); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 996 | RMW_BITS(bgp, i, tshut_threshold, |
Eduardo Valentin | d3c291a | 2013-03-15 08:59:55 -0400 | [diff] [blame] | 997 | tshut_cold_mask, ts_data->tshut_cold); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1001 | if (TI_BANDGAP_HAS(bgp, MODE_CONFIG)) |
| 1002 | ti_bandgap_set_continuous_mode(bgp); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1003 | |
| 1004 | /* Set .250 seconds time as default counter */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1005 | if (TI_BANDGAP_HAS(bgp, COUNTER)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1006 | for (i = 0; i < bgp->conf->sensor_count; i++) |
| 1007 | RMW_BITS(bgp, i, bgap_counter, counter_mask, |
| 1008 | bgp->clk_rate / 4); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1009 | |
| 1010 | /* Every thing is good? Then expose the sensors */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1011 | for (i = 0; i < bgp->conf->sensor_count; i++) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1012 | char *domain; |
| 1013 | |
Eduardo Valentin | f155333 | 2013-04-08 08:19:13 -0400 | [diff] [blame] | 1014 | if (bgp->conf->sensors[i].register_cooling) { |
| 1015 | ret = bgp->conf->sensors[i].register_cooling(bgp, i); |
| 1016 | if (ret) |
| 1017 | goto remove_sensors; |
| 1018 | } |
Eduardo Valentin | 04a4d10 | 2012-09-11 19:06:55 +0300 | [diff] [blame] | 1019 | |
Eduardo Valentin | f155333 | 2013-04-08 08:19:13 -0400 | [diff] [blame] | 1020 | if (bgp->conf->expose_sensor) { |
| 1021 | domain = bgp->conf->sensors[i].domain; |
| 1022 | ret = bgp->conf->expose_sensor(bgp, i, domain); |
| 1023 | if (ret) |
| 1024 | goto remove_last_cooling; |
| 1025 | } |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | /* |
| 1029 | * Enable the Interrupts once everything is set. Otherwise irq handler |
| 1030 | * might be called as soon as it is enabled where as rest of framework |
| 1031 | * is still getting initialised. |
| 1032 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1033 | if (TI_BANDGAP_HAS(bgp, TALERT)) { |
| 1034 | ret = ti_bandgap_talert_init(bgp, pdev); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1035 | if (ret) { |
| 1036 | dev_err(&pdev->dev, "failed to initialize Talert IRQ\n"); |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1037 | i = bgp->conf->sensor_count; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1038 | goto disable_clk; |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | return 0; |
| 1043 | |
Eduardo Valentin | f155333 | 2013-04-08 08:19:13 -0400 | [diff] [blame] | 1044 | remove_last_cooling: |
| 1045 | if (bgp->conf->sensors[i].unregister_cooling) |
| 1046 | bgp->conf->sensors[i].unregister_cooling(bgp, i); |
| 1047 | remove_sensors: |
| 1048 | for (i--; i >= 0; i--) { |
| 1049 | if (bgp->conf->sensors[i].unregister_cooling) |
| 1050 | bgp->conf->sensors[i].unregister_cooling(bgp, i); |
| 1051 | if (bgp->conf->remove_sensor) |
| 1052 | bgp->conf->remove_sensor(bgp, i); |
| 1053 | } |
| 1054 | ti_bandgap_power(bgp, false); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1055 | disable_clk: |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1056 | if (TI_BANDGAP_HAS(bgp, CLK_CTRL)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1057 | clk_disable_unprepare(bgp->fclock); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1058 | put_clks: |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1059 | clk_put(bgp->div_clk); |
Luis Henriques | 882f581 | 2016-11-16 22:15:22 +0000 | [diff] [blame] | 1060 | put_fclock: |
| 1061 | clk_put(bgp->fclock); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1062 | free_irqs: |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1063 | if (TI_BANDGAP_HAS(bgp, TSHUT)) { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1064 | free_irq(gpio_to_irq(bgp->tshut_gpio), NULL); |
| 1065 | gpio_free(bgp->tshut_gpio); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | return ret; |
| 1069 | } |
| 1070 | |
| 1071 | static |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1072 | int ti_bandgap_remove(struct platform_device *pdev) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1073 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1074 | struct ti_bandgap *bgp = platform_get_drvdata(pdev); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1075 | int i; |
| 1076 | |
| 1077 | /* First thing is to remove sensor interfaces */ |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1078 | for (i = 0; i < bgp->conf->sensor_count; i++) { |
Eduardo Valentin | 262235b | 2013-04-08 08:19:14 -0400 | [diff] [blame] | 1079 | if (bgp->conf->sensors[i].unregister_cooling) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1080 | bgp->conf->sensors[i].unregister_cooling(bgp, i); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1081 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1082 | if (bgp->conf->remove_sensor) |
| 1083 | bgp->conf->remove_sensor(bgp, i); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1084 | } |
| 1085 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1086 | ti_bandgap_power(bgp, false); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1087 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1088 | if (TI_BANDGAP_HAS(bgp, CLK_CTRL)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1089 | clk_disable_unprepare(bgp->fclock); |
| 1090 | clk_put(bgp->fclock); |
| 1091 | clk_put(bgp->div_clk); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1092 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1093 | if (TI_BANDGAP_HAS(bgp, TALERT)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1094 | free_irq(bgp->irq, bgp); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1095 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1096 | if (TI_BANDGAP_HAS(bgp, TSHUT)) { |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1097 | free_irq(gpio_to_irq(bgp->tshut_gpio), NULL); |
| 1098 | gpio_free(bgp->tshut_gpio); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | return 0; |
| 1102 | } |
| 1103 | |
Grygorii Strashko | 3992b62 | 2015-02-06 16:55:46 +0200 | [diff] [blame] | 1104 | #ifdef CONFIG_PM_SLEEP |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1105 | static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1106 | { |
| 1107 | int i; |
| 1108 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1109 | for (i = 0; i < bgp->conf->sensor_count; i++) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1110 | struct temp_sensor_registers *tsr; |
| 1111 | struct temp_sensor_regval *rval; |
| 1112 | |
Eduardo Valentin | 9879b2c | 2013-03-19 10:54:23 -0400 | [diff] [blame] | 1113 | rval = &bgp->regval[i]; |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1114 | tsr = bgp->conf->sensors[i].registers; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1115 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1116 | if (TI_BANDGAP_HAS(bgp, MODE_CONFIG)) |
| 1117 | rval->bg_mode_ctrl = ti_bandgap_readl(bgp, |
J Keerthy | 76d2cd3 | 2012-09-11 19:06:52 +0300 | [diff] [blame] | 1118 | tsr->bgap_mode_ctrl); |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1119 | if (TI_BANDGAP_HAS(bgp, COUNTER)) |
| 1120 | rval->bg_counter = ti_bandgap_readl(bgp, |
J Keerthy | 76d2cd3 | 2012-09-11 19:06:52 +0300 | [diff] [blame] | 1121 | tsr->bgap_counter); |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1122 | if (TI_BANDGAP_HAS(bgp, TALERT)) { |
| 1123 | rval->bg_threshold = ti_bandgap_readl(bgp, |
J Keerthy | 76d2cd3 | 2012-09-11 19:06:52 +0300 | [diff] [blame] | 1124 | tsr->bgap_threshold); |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1125 | rval->bg_ctrl = ti_bandgap_readl(bgp, |
J Keerthy | 76d2cd3 | 2012-09-11 19:06:52 +0300 | [diff] [blame] | 1126 | tsr->bgap_mask_ctrl); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1127 | } |
| 1128 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1129 | if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) |
| 1130 | rval->tshut_threshold = ti_bandgap_readl(bgp, |
J Keerthy | 76d2cd3 | 2012-09-11 19:06:52 +0300 | [diff] [blame] | 1131 | tsr->tshut_threshold); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1132 | } |
| 1133 | |
| 1134 | return 0; |
| 1135 | } |
| 1136 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1137 | static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1138 | { |
| 1139 | int i; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1140 | |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1141 | for (i = 0; i < bgp->conf->sensor_count; i++) { |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1142 | struct temp_sensor_registers *tsr; |
| 1143 | struct temp_sensor_regval *rval; |
| 1144 | u32 val = 0; |
| 1145 | |
Eduardo Valentin | 9879b2c | 2013-03-19 10:54:23 -0400 | [diff] [blame] | 1146 | rval = &bgp->regval[i]; |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1147 | tsr = bgp->conf->sensors[i].registers; |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1148 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1149 | if (TI_BANDGAP_HAS(bgp, COUNTER)) |
| 1150 | val = ti_bandgap_readl(bgp, tsr->bgap_counter); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1151 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1152 | if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) |
| 1153 | ti_bandgap_writel(bgp, rval->tshut_threshold, |
| 1154 | tsr->tshut_threshold); |
Radhesh Fadnis | b87ea75 | 2012-11-13 14:10:04 -0400 | [diff] [blame] | 1155 | /* Force immediate temperature measurement and update |
| 1156 | * of the DTEMP field |
| 1157 | */ |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1158 | ti_bandgap_force_single_read(bgp, i); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1159 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1160 | if (TI_BANDGAP_HAS(bgp, COUNTER)) |
| 1161 | ti_bandgap_writel(bgp, rval->bg_counter, |
| 1162 | tsr->bgap_counter); |
| 1163 | if (TI_BANDGAP_HAS(bgp, MODE_CONFIG)) |
| 1164 | ti_bandgap_writel(bgp, rval->bg_mode_ctrl, |
| 1165 | tsr->bgap_mode_ctrl); |
| 1166 | if (TI_BANDGAP_HAS(bgp, TALERT)) { |
| 1167 | ti_bandgap_writel(bgp, rval->bg_threshold, |
| 1168 | tsr->bgap_threshold); |
| 1169 | ti_bandgap_writel(bgp, rval->bg_ctrl, |
| 1170 | tsr->bgap_mask_ctrl); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1177 | static int ti_bandgap_suspend(struct device *dev) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1178 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1179 | struct ti_bandgap *bgp = dev_get_drvdata(dev); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1180 | int err; |
| 1181 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1182 | err = ti_bandgap_save_ctxt(bgp); |
| 1183 | ti_bandgap_power(bgp, false); |
Radhesh Fadnis | 6c9c1d6 | 2013-02-26 18:53:25 -0400 | [diff] [blame] | 1184 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1185 | if (TI_BANDGAP_HAS(bgp, CLK_CTRL)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1186 | clk_disable_unprepare(bgp->fclock); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1187 | |
| 1188 | return err; |
| 1189 | } |
| 1190 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1191 | static int ti_bandgap_resume(struct device *dev) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1192 | { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1193 | struct ti_bandgap *bgp = dev_get_drvdata(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, CLK_CTRL)) |
Eduardo Valentin | d7f080e | 2013-03-19 10:54:18 -0400 | [diff] [blame] | 1196 | clk_prepare_enable(bgp->fclock); |
Radhesh Fadnis | 6c9c1d6 | 2013-02-26 18:53:25 -0400 | [diff] [blame] | 1197 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1198 | ti_bandgap_power(bgp, true); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1199 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1200 | return ti_bandgap_restore_ctxt(bgp); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1201 | } |
Jingoo Han | 5204f8c | 2014-02-27 20:43:02 +0900 | [diff] [blame] | 1202 | static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend, |
| 1203 | ti_bandgap_resume); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1204 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1205 | #define DEV_PM_OPS (&ti_bandgap_dev_pm_ops) |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1206 | #else |
| 1207 | #define DEV_PM_OPS NULL |
| 1208 | #endif |
| 1209 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1210 | static const struct of_device_id of_ti_bandgap_match[] = { |
Pavel Machek | 9c5c87e | 2015-04-02 16:49:07 +0200 | [diff] [blame] | 1211 | #ifdef CONFIG_OMAP3_THERMAL |
| 1212 | { |
| 1213 | .compatible = "ti,omap34xx-bandgap", |
| 1214 | .data = (void *)&omap34xx_data, |
| 1215 | }, |
Eduardo Valentin | b840b6e | 2015-09-21 17:32:15 -0700 | [diff] [blame] | 1216 | { |
| 1217 | .compatible = "ti,omap36xx-bandgap", |
| 1218 | .data = (void *)&omap36xx_data, |
| 1219 | }, |
Pavel Machek | 9c5c87e | 2015-04-02 16:49:07 +0200 | [diff] [blame] | 1220 | #endif |
Eduardo Valentin | 1a31270 | 2012-07-12 19:02:31 +0300 | [diff] [blame] | 1221 | #ifdef CONFIG_OMAP4_THERMAL |
| 1222 | { |
| 1223 | .compatible = "ti,omap4430-bandgap", |
| 1224 | .data = (void *)&omap4430_data, |
| 1225 | }, |
| 1226 | { |
| 1227 | .compatible = "ti,omap4460-bandgap", |
| 1228 | .data = (void *)&omap4460_data, |
| 1229 | }, |
| 1230 | { |
| 1231 | .compatible = "ti,omap4470-bandgap", |
| 1232 | .data = (void *)&omap4470_data, |
| 1233 | }, |
| 1234 | #endif |
Eduardo Valentin | 949f5a5 | 2012-07-12 19:02:32 +0300 | [diff] [blame] | 1235 | #ifdef CONFIG_OMAP5_THERMAL |
| 1236 | { |
| 1237 | .compatible = "ti,omap5430-bandgap", |
| 1238 | .data = (void *)&omap5430_data, |
| 1239 | }, |
| 1240 | #endif |
Eduardo Valentin | 25870e6 | 2013-05-29 15:07:45 +0000 | [diff] [blame] | 1241 | #ifdef CONFIG_DRA752_THERMAL |
| 1242 | { |
| 1243 | .compatible = "ti,dra752-bandgap", |
| 1244 | .data = (void *)&dra752_data, |
| 1245 | }, |
| 1246 | #endif |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1247 | /* Sentinel */ |
| 1248 | { }, |
| 1249 | }; |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1250 | MODULE_DEVICE_TABLE(of, of_ti_bandgap_match); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1251 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1252 | static struct platform_driver ti_bandgap_sensor_driver = { |
| 1253 | .probe = ti_bandgap_probe, |
| 1254 | .remove = ti_bandgap_remove, |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1255 | .driver = { |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1256 | .name = "ti-soc-thermal", |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1257 | .pm = DEV_PM_OPS, |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1258 | .of_match_table = of_ti_bandgap_match, |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1259 | }, |
| 1260 | }; |
| 1261 | |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1262 | module_platform_driver(ti_bandgap_sensor_driver); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1263 | |
| 1264 | MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver"); |
| 1265 | MODULE_LICENSE("GPL v2"); |
Eduardo Valentin | 03e859d | 2013-03-19 10:54:21 -0400 | [diff] [blame] | 1266 | MODULE_ALIAS("platform:ti-soc-thermal"); |
Eduardo Valentin | 8feaf0ce | 2012-07-12 19:02:29 +0300 | [diff] [blame] | 1267 | MODULE_AUTHOR("Texas Instrument Inc."); |