blob: 12f3ee5337bb7d93e854b8c15ab1cf4d5e408f67 [file] [log] [blame]
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001/*
Eduardo Valentin03e859d2013-03-19 10:54:21 -04002 * TI Bandgap temperature sensor driver
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03003 *
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 Valentinebf0bd52013-03-15 09:00:35 -040036#include <linux/spinlock.h>
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030037#include <linux/reboot.h>
38#include <linux/of_device.h>
39#include <linux/of_platform.h>
40#include <linux/of_irq.h>
Eduardo Valentin57d16172013-06-07 11:11:53 -040041#include <linux/of_gpio.h>
Eduardo Valentin2aeeb8a2012-11-13 14:10:00 -040042#include <linux/io.h>
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030043
Eduardo Valentin7372add2013-03-19 10:54:19 -040044#include "ti-bandgap.h"
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030045
Eduardo Valentin8abbe712013-03-15 09:00:05 -040046/*** Helper functions to access registers and their bitfields ***/
47
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040048/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -040049 * ti_bandgap_readl() - simple read helper function
50 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040051 * @reg: desired register (offset) to be read
52 *
53 * Helper function to read bandgap registers. It uses the io remapped area.
Nishanth Menon169e8d02013-04-01 12:04:34 -040054 * Return: the register value.
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040055 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -040056static u32 ti_bandgap_readl(struct ti_bandgap *bgp, u32 reg)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030057{
Eduardo Valentind7f080e2013-03-19 10:54:18 -040058 return readl(bgp->base + reg);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030059}
60
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040061/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -040062 * ti_bandgap_writel() - simple write helper function
63 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040064 * @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 Valentin03e859d2013-03-19 10:54:21 -040069static void ti_bandgap_writel(struct ti_bandgap *bgp, u32 val, u32 reg)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030070{
Eduardo Valentind7f080e2013-03-19 10:54:18 -040071 writel(val, bgp->base + reg);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +030072}
73
Eduardo Valentin9c468aa2013-03-15 08:59:56 -040074/**
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 Valentind7f080e2013-03-19 10:54:18 -040080#define RMW_BITS(bgp, id, reg, mask, val) \
Eduardo Valentind3c291a2013-03-15 08:59:55 -040081do { \
82 struct temp_sensor_registers *t; \
83 u32 r; \
84 \
Eduardo Valentind7f080e2013-03-19 10:54:18 -040085 t = bgp->conf->sensors[(id)].registers; \
Eduardo Valentin03e859d2013-03-19 10:54:21 -040086 r = ti_bandgap_readl(bgp, t->reg); \
Eduardo Valentind3c291a2013-03-15 08:59:55 -040087 r &= ~t->mask; \
88 r |= (val) << __ffs(t->mask); \
Eduardo Valentin03e859d2013-03-19 10:54:21 -040089 ti_bandgap_writel(bgp, r, t->reg); \
Eduardo Valentind3c291a2013-03-15 08:59:55 -040090} while (0)
91
Eduardo Valentin6ab52402013-03-15 09:00:06 -040092/*** Basic helper functions ***/
93
Eduardo Valentin7a556e62013-03-15 08:59:58 -040094/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -040095 * ti_bandgap_power() - controls the power state of a bandgap device
96 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin7a556e62013-03-15 08:59:58 -040097 * @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 Menon169e8d02013-04-01 12:04:34 -0400101 *
102 * Return: 0 on success, -ENOTSUPP if tempsoff is not supported.
Eduardo Valentin7a556e62013-03-15 08:59:58 -0400103 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400104static int ti_bandgap_power(struct ti_bandgap *bgp, bool on)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300105{
Pavel Macheke34238b2015-01-18 21:17:10 +0100106 int i;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300107
Pavel Macheke34238b2015-01-18 21:17:10 +0100108 if (!TI_BANDGAP_HAS(bgp, POWER_SWITCH))
109 return -ENOTSUPP;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300110
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400111 for (i = 0; i < bgp->conf->sensor_count; i++)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300112 /* active on 0 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400113 RMW_BITS(bgp, i, temp_sensor_ctrl, bgap_tempsoff_mask, !on);
Pavel Macheke34238b2015-01-18 21:17:10 +0100114 return 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300115}
116
Eduardo Valentin4a6554e2013-03-15 08:59:59 -0400117/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400118 * ti_bandgap_read_temp() - helper function to read sensor temperature
119 * @bgp: pointer to ti_bandgap structure
Eduardo Valentin4a6554e2013-03-15 08:59:59 -0400120 * @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 Menon169e8d02013-04-01 12:04:34 -0400126 *
127 * Return: temperature in ADC values.
Eduardo Valentin4a6554e2013-03-15 08:59:59 -0400128 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400129static u32 ti_bandgap_read_temp(struct ti_bandgap *bgp, int id)
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400130{
131 struct temp_sensor_registers *tsr;
Eduardo Valentind3c291a2013-03-15 08:59:55 -0400132 u32 temp, reg;
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400133
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400134 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400135 reg = tsr->temp_sensor_ctrl;
136
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400137 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400138 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400139 /*
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 Valentin03e859d2013-03-19 10:54:21 -0400147 temp = ti_bandgap_readl(bgp, reg);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400148 temp &= tsr->bgap_dtemp_mask;
149
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400150 if (TI_BANDGAP_HAS(bgp, FREEZE_BIT))
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400151 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400152
153 return temp;
154}
155
Eduardo Valentinfb65b882013-03-15 09:00:07 -0400156/*** IRQ handlers ***/
157
Eduardo Valentinee07d552013-03-15 09:00:01 -0400158/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400159 * ti_bandgap_talert_irq_handler() - handles Temperature alert IRQs
Eduardo Valentinee07d552013-03-15 09:00:01 -0400160 * @irq: IRQ number
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400161 * @data: private data (struct ti_bandgap *)
Eduardo Valentinee07d552013-03-15 09:00:01 -0400162 *
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 Menon169e8d02013-04-01 12:04:34 -0400168 *
169 * Return: IRQ_HANDLED
Eduardo Valentinee07d552013-03-15 09:00:01 -0400170 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400171static irqreturn_t ti_bandgap_talert_irq_handler(int irq, void *data)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300172{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400173 struct ti_bandgap *bgp = data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300174 struct temp_sensor_registers *tsr;
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400175 u32 t_hot = 0, t_cold = 0, ctrl;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300176 int i;
177
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400178 spin_lock(&bgp->lock);
179 for (i = 0; i < bgp->conf->sensor_count; i++) {
180 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400181 ctrl = ti_bandgap_readl(bgp, tsr->bgap_status);
Eduardo Valentine555c952013-03-15 09:00:04 -0400182
183 /* Read the status of t_hot */
184 t_hot = ctrl & tsr->status_hot_mask;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300185
186 /* Read the status of t_cold */
Eduardo Valentine555c952013-03-15 09:00:04 -0400187 t_cold = ctrl & tsr->status_cold_mask;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300188
189 if (!t_cold && !t_hot)
190 continue;
191
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400192 ctrl = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300193 /*
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 Valentin03e859d2013-03-19 10:54:21 -0400206 ti_bandgap_writel(bgp, ctrl, tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300207
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400208 dev_dbg(bgp->dev,
Eduardo Valentin71e303f2012-11-13 14:10:03 -0400209 "%s: IRQ from %s sensor: hotevent %d coldevent %d\n",
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400210 __func__, bgp->conf->sensors[i].domain,
Eduardo Valentin71e303f2012-11-13 14:10:03 -0400211 t_hot, t_cold);
212
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300213 /* report temperature to whom may concern */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400214 if (bgp->conf->report_temperature)
215 bgp->conf->report_temperature(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300216 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400217 spin_unlock(&bgp->lock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300218
219 return IRQ_HANDLED;
220}
221
Eduardo Valentin79857cd22013-03-15 09:00:02 -0400222/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400223 * ti_bandgap_tshut_irq_handler() - handles Temperature shutdown signal
Eduardo Valentin79857cd22013-03-15 09:00:02 -0400224 * @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 Menon169e8d02013-04-01 12:04:34 -0400230 *
231 * Return: IRQ_HANDLED
Eduardo Valentin79857cd22013-03-15 09:00:02 -0400232 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400233static irqreturn_t ti_bandgap_tshut_irq_handler(int irq, void *data)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300234{
Ruslan Ruslichenkob3bf0e92013-02-26 18:53:24 -0400235 pr_emerg("%s: TSHUT temperature reached. Needs shut down...\n",
236 __func__);
237
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300238 orderly_poweroff(true);
239
240 return IRQ_HANDLED;
241}
242
Eduardo Valentin2f6af4b2013-03-15 09:00:08 -0400243/*** Helper functions which manipulate conversion ADC <-> mi Celsius ***/
244
Eduardo Valentin2577e932013-03-15 09:00:11 -0400245/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400246 * ti_bandgap_adc_to_mcelsius() - converts an ADC value to mCelsius scale
247 * @bgp: struct ti_bandgap pointer
Eduardo Valentin2577e932013-03-15 09:00:11 -0400248 * @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 Menon169e8d02013-04-01 12:04:34 -0400254 *
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 Valentin2577e932013-03-15 09:00:11 -0400257 */
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300258static
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400259int ti_bandgap_adc_to_mcelsius(struct ti_bandgap *bgp, int adc_val, int *t)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300260{
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400261 const struct ti_bandgap_data *conf = bgp->conf;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300262
263 /* look up for temperature in the table and return the temperature */
Pavel Macheke34238b2015-01-18 21:17:10 +0100264 if (adc_val < conf->adc_start_val || adc_val > conf->adc_end_val)
265 return -ERANGE;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300266
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400267 *t = bgp->conf->conv_table[adc_val - conf->adc_start_val];
Pavel Macheke34238b2015-01-18 21:17:10 +0100268 return 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300269}
270
Eduardo Valentine7f60b52013-03-15 09:00:15 -0400271/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400272 * ti_bandgap_mcelsius_to_adc() - converts a mCelsius value to ADC scale
273 * @bgp: struct ti_bandgap pointer
Eduardo Valentine7f60b52013-03-15 09:00:15 -0400274 * @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 Menon169e8d02013-04-01 12:04:34 -0400280 *
281 * Return: 0 if conversion was successful, else -ERANGE in case the @temp
282 * argument is out of the ADC conv table range.
Eduardo Valentine7f60b52013-03-15 09:00:15 -0400283 */
Eduardo Valentine16f0722013-03-15 09:00:12 -0400284static
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400285int ti_bandgap_mcelsius_to_adc(struct ti_bandgap *bgp, long temp, int *adc)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300286{
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400287 const struct ti_bandgap_data *conf = bgp->conf;
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400288 const int *conv_table = bgp->conf->conv_table;
Pavel Macheke34238b2015-01-18 21:17:10 +0100289 int high, low, mid;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300290
291 low = 0;
Eduardo Valentin26a70ed2013-03-15 09:00:14 -0400292 high = conf->adc_end_val - conf->adc_start_val;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300293 mid = (high + low) / 2;
294
Pavel Macheke34238b2015-01-18 21:17:10 +0100295 if (temp < conv_table[low] || temp > conv_table[high])
296 return -ERANGE;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300297
298 while (low < high) {
Eduardo Valentinc8a8f842013-02-26 18:53:36 -0400299 if (temp < conv_table[mid])
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300300 high = mid - 1;
301 else
302 low = mid + 1;
303 mid = (low + high) / 2;
304 }
305
Eduardo Valentin26a70ed2013-03-15 09:00:14 -0400306 *adc = conf->adc_start_val + low;
Pavel Macheke34238b2015-01-18 21:17:10 +0100307 return 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300308}
309
Eduardo Valentin8a1cefe2013-03-15 09:00:17 -0400310/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400311 * ti_bandgap_add_hyst() - add hysteresis (in mCelsius) to an ADC value
312 * @bgp: struct ti_bandgap pointer
Eduardo Valentin8a1cefe2013-03-15 09:00:17 -0400313 * @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 Menon169e8d02013-04-01 12:04:34 -0400318 *
319 * Return: 0 on success, -ERANGE otherwise.
Eduardo Valentin8a1cefe2013-03-15 09:00:17 -0400320 */
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400321static
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400322int ti_bandgap_add_hyst(struct ti_bandgap *bgp, int adc_val, int hyst_val,
323 u32 *sum)
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400324{
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 Valentin03e859d2013-03-19 10:54:21 -0400331 ret = ti_bandgap_adc_to_mcelsius(bgp, adc_val, &temp);
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400332 if (ret < 0)
Pavel Macheke34238b2015-01-18 21:17:10 +0100333 return ret;
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400334
335 temp += hyst_val;
336
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400337 ret = ti_bandgap_mcelsius_to_adc(bgp, temp, sum);
Eduardo Valentin0f0ed7d2013-03-15 09:00:16 -0400338 return ret;
339}
340
Eduardo Valentinf8ccce22013-03-15 09:00:18 -0400341/*** Helper functions handling device Alert/Shutdown signals ***/
342
Eduardo Valentinf47f6d32013-03-15 09:00:20 -0400343/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400344 * ti_bandgap_unmask_interrupts() - unmasks the events of thot & tcold
345 * @bgp: struct ti_bandgap pointer
Eduardo Valentin61603af2013-03-19 10:54:25 -0400346 * @id: bandgap sensor id
Eduardo Valentinf47f6d32013-03-15 09:00:20 -0400347 * @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 Valentin03e859d2013-03-19 10:54:21 -0400353static void ti_bandgap_unmask_interrupts(struct ti_bandgap *bgp, int id,
354 u32 t_hot, u32 t_cold)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300355{
356 struct temp_sensor_registers *tsr;
357 u32 temp, reg_val;
358
359 /* Read the current on die temperature */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400360 temp = ti_bandgap_read_temp(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300361
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400362 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400363 reg_val = ti_bandgap_readl(bgp, tsr->bgap_mask_ctrl);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400364
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300365 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 Valentin03e859d2013-03-19 10:54:21 -0400374 ti_bandgap_writel(bgp, reg_val, tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300375}
376
Eduardo Valentin38d99e82013-03-15 09:00:27 -0400377/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400378 * ti_bandgap_update_alert_threshold() - sequence to update thresholds
379 * @bgp: struct ti_bandgap pointer
Eduardo Valentin38d99e82013-03-15 09:00:27 -0400380 * @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 Menon169e8d02013-04-01 12:04:34 -0400390 *
391 * Return: 0 if no error, else corresponding error
Eduardo Valentin38d99e82013-03-15 09:00:27 -0400392 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400393static int ti_bandgap_update_alert_threshold(struct ti_bandgap *bgp, int id,
394 int val, bool hot)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300395{
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400396 struct temp_sensor_data *ts_data = bgp->conf->sensors[id].ts_data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300397 struct temp_sensor_registers *tsr;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400398 u32 thresh_val, reg_val, t_hot, t_cold;
399 int err = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300400
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400401 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300402
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400403 /* obtain the current value */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400404 thresh_val = ti_bandgap_readl(bgp, tsr->bgap_threshold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400405 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 Valentinf5d43b72013-03-19 10:54:26 -0400414 if (t_cold > t_hot) {
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400415 if (hot)
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400416 err = ti_bandgap_add_hyst(bgp, t_hot,
417 -ts_data->hyst_val,
418 &t_cold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400419 else
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400420 err = ti_bandgap_add_hyst(bgp, t_cold,
421 ts_data->hyst_val,
422 &t_hot);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300423 }
424
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400425 /* write the new threshold values */
Eduardo Valentin0fb3c242013-03-19 10:54:27 -0400426 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 Valentin03e859d2013-03-19 10:54:21 -0400430 ti_bandgap_writel(bgp, reg_val, tsr->bgap_threshold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400431
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300432 if (err) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400433 dev_err(bgp->dev, "failed to reprogram thot threshold\n");
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400434 err = -EIO;
435 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300436 }
437
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400438 ti_bandgap_unmask_interrupts(bgp, id, t_hot, t_cold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400439exit:
440 return err;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300441}
442
Eduardo Valentine72b7bb2013-03-15 09:00:38 -0400443/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400444 * ti_bandgap_validate() - helper to check the sanity of a struct ti_bandgap
445 * @bgp: struct ti_bandgap pointer
Eduardo Valentine72b7bb2013-03-15 09:00:38 -0400446 * @id: bandgap sensor id
447 *
448 * Checks if the bandgap pointer is valid and if the sensor id is also
449 * applicable.
Nishanth Menon169e8d02013-04-01 12:04:34 -0400450 *
451 * Return: 0 if no errors, -EINVAL for invalid @bgp pointer or -ERANGE if
452 * @id cannot index @bgp sensors.
Eduardo Valentine72b7bb2013-03-15 09:00:38 -0400453 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400454static inline int ti_bandgap_validate(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300455{
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +0000456 if (!bgp || IS_ERR(bgp)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300457 pr_err("%s: invalid bandgap pointer\n", __func__);
Pavel Macheke34238b2015-01-18 21:17:10 +0100458 return -EINVAL;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300459 }
460
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400461 if ((id < 0) || (id >= bgp->conf->sensor_count)) {
462 dev_err(bgp->dev, "%s: sensor id out of range (%d)\n",
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300463 __func__, id);
Pavel Macheke34238b2015-01-18 21:17:10 +0100464 return -ERANGE;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300465 }
466
Pavel Macheke34238b2015-01-18 21:17:10 +0100467 return 0;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400468}
469
Eduardo Valentin9efa93b2013-03-15 09:00:28 -0400470/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400471 * _ti_bandgap_write_threshold() - helper to update TALERT t_cold or t_hot
472 * @bgp: struct ti_bandgap pointer
Eduardo Valentin9efa93b2013-03-15 09:00:28 -0400473 * @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 Menon169e8d02013-04-01 12:04:34 -0400482 *
483 * Return: 0 if no error, else corresponding error value.
Eduardo Valentin9efa93b2013-03-15 09:00:28 -0400484 */
Eduardo Valentin2f8ec2a2013-03-19 10:54:22 -0400485static int _ti_bandgap_write_threshold(struct ti_bandgap *bgp, int id, int val,
486 bool hot)
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400487{
488 struct temp_sensor_data *ts_data;
489 struct temp_sensor_registers *tsr;
490 u32 adc_val;
491 int ret;
492
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400493 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400494 if (ret)
Pavel Macheke34238b2015-01-18 21:17:10 +0100495 return ret;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400496
Pavel Macheke34238b2015-01-18 21:17:10 +0100497 if (!TI_BANDGAP_HAS(bgp, TALERT))
498 return -ENOTSUPP;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400499
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400500 ts_data = bgp->conf->sensors[id].ts_data;
501 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400502 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 Macheke34238b2015-01-18 21:17:10 +0100511 return ret;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400512
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400513 ret = ti_bandgap_mcelsius_to_adc(bgp, val, &adc_val);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400514 if (ret < 0)
Pavel Macheke34238b2015-01-18 21:17:10 +0100515 return ret;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400516
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400517 spin_lock(&bgp->lock);
Eduardo Valentind52361c2013-03-19 10:54:28 -0400518 ret = ti_bandgap_update_alert_threshold(bgp, id, adc_val, hot);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400519 spin_unlock(&bgp->lock);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400520 return ret;
521}
522
Eduardo Valentin7a681a52013-03-15 09:00:29 -0400523/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400524 * _ti_bandgap_read_threshold() - helper to read TALERT t_cold or t_hot
525 * @bgp: struct ti_bandgap pointer
Eduardo Valentin7a681a52013-03-15 09:00:29 -0400526 * @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 Menon169e8d02013-04-01 12:04:34 -0400534 *
535 * Return: 0 if no error, -ENOTSUPP if it has no TALERT support, or the
536 * corresponding error value if some operation fails.
Eduardo Valentin7a681a52013-03-15 09:00:29 -0400537 */
Eduardo Valentin2f8ec2a2013-03-19 10:54:22 -0400538static int _ti_bandgap_read_threshold(struct ti_bandgap *bgp, int id,
539 int *val, bool hot)
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400540{
541 struct temp_sensor_registers *tsr;
542 u32 temp, mask;
543 int ret = 0;
544
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400545 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400546 if (ret)
547 goto exit;
548
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400549 if (!TI_BANDGAP_HAS(bgp, TALERT)) {
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400550 ret = -ENOTSUPP;
551 goto exit;
552 }
553
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400554 tsr = bgp->conf->sensors[id].registers;
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400555 if (hot)
556 mask = tsr->threshold_thot_mask;
557 else
558 mask = tsr->threshold_tcold_mask;
559
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400560 temp = ti_bandgap_readl(bgp, tsr->bgap_threshold);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400561 temp = (temp & mask) >> __ffs(mask);
Pavel Macheke34238b2015-01-18 21:17:10 +0100562 ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400563 if (ret) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400564 dev_err(bgp->dev, "failed to read thot\n");
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400565 ret = -EIO;
566 goto exit;
567 }
568
569 *val = temp;
570
571exit:
Eduardo Valentin648b4c62013-03-19 10:54:17 -0400572 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300573}
574
Eduardo Valentin56f132f2013-03-15 09:00:21 -0400575/*** Exposed APIs ***/
576
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300577/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400578 * ti_bandgap_read_thot() - reads sensor current thot
Eduardo Valentin61603af2013-03-19 10:54:25 -0400579 * @bgp: pointer to bandgap instance
580 * @id: sensor id
581 * @thot: resulting current thot value
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300582 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400583 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300584 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400585int ti_bandgap_read_thot(struct ti_bandgap *bgp, int id, int *thot)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300586{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400587 return _ti_bandgap_read_threshold(bgp, id, thot, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300588}
589
590/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400591 * ti_bandgap_write_thot() - sets sensor current thot
Eduardo Valentin61603af2013-03-19 10:54:25 -0400592 * @bgp: pointer to bandgap instance
593 * @id: sensor id
594 * @val: desired thot value
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300595 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400596 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300597 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400598int ti_bandgap_write_thot(struct ti_bandgap *bgp, int id, int val)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300599{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400600 return _ti_bandgap_write_threshold(bgp, id, val, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300601}
602
603/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400604 * ti_bandgap_read_tcold() - reads sensor current tcold
Eduardo Valentin61603af2013-03-19 10:54:25 -0400605 * @bgp: pointer to bandgap instance
606 * @id: sensor id
607 * @tcold: resulting current tcold value
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300608 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400609 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300610 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400611int ti_bandgap_read_tcold(struct ti_bandgap *bgp, int id, int *tcold)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300612{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400613 return _ti_bandgap_read_threshold(bgp, id, tcold, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300614}
615
616/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400617 * ti_bandgap_write_tcold() - sets the sensor tcold
Eduardo Valentin61603af2013-03-19 10:54:25 -0400618 * @bgp: pointer to bandgap instance
619 * @id: sensor id
620 * @val: desired tcold value
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300621 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400622 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300623 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400624int ti_bandgap_write_tcold(struct ti_bandgap *bgp, int id, int val)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300625{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400626 return _ti_bandgap_write_threshold(bgp, id, val, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300627}
628
629/**
J Keerthy58bccd02013-04-01 12:04:42 -0400630 * 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 */
635static 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 */
655static 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 Valentin03e859d2013-03-19 10:54:21 -0400692 * ti_bandgap_read_update_interval() - read the sensor update interval
Eduardo Valentin61603af2013-03-19 10:54:25 -0400693 * @bgp: pointer to bandgap instance
694 * @id: sensor id
695 * @interval: resulting update interval in miliseconds
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300696 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400697 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300698 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400699int ti_bandgap_read_update_interval(struct ti_bandgap *bgp, int id,
700 int *interval)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300701{
J Keerthy58bccd02013-04-01 12:04:42 -0400702 int ret = 0;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300703
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400704 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300705 if (ret)
J Keerthy58bccd02013-04-01 12:04:42 -0400706 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300707
J Keerthy58bccd02013-04-01 12:04:42 -0400708 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
709 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
710 ret = -ENOTSUPP;
711 goto exit;
712 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300713
J Keerthy58bccd02013-04-01 12:04:42 -0400714 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
715 ti_bandgap_read_counter(bgp, id, interval);
716 goto exit;
717 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300718
J Keerthy58bccd02013-04-01 12:04:42 -0400719 ti_bandgap_read_counter_delay(bgp, id, interval);
720exit:
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 */
732static 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 Valentin8feaf0ce2012-07-12 19:02:29 +0300764
765 return 0;
766}
767
768/**
J Keerthy58bccd02013-04-01 12:04:42 -0400769 * 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 */
774static 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 Valentin03e859d2013-03-19 10:54:21 -0400784 * ti_bandgap_write_update_interval() - set the update interval
Eduardo Valentin61603af2013-03-19 10:54:25 -0400785 * @bgp: pointer to bandgap instance
786 * @id: sensor id
787 * @interval: desired update interval in miliseconds
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300788 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400789 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300790 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400791int ti_bandgap_write_update_interval(struct ti_bandgap *bgp,
792 int id, u32 interval)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300793{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400794 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300795 if (ret)
J Keerthy58bccd02013-04-01 12:04:42 -0400796 goto exit;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300797
J Keerthy58bccd02013-04-01 12:04:42 -0400798 if (!TI_BANDGAP_HAS(bgp, COUNTER) &&
799 !TI_BANDGAP_HAS(bgp, COUNTER_DELAY)) {
800 ret = -ENOTSUPP;
801 goto exit;
802 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300803
J Keerthy58bccd02013-04-01 12:04:42 -0400804 if (TI_BANDGAP_HAS(bgp, COUNTER)) {
805 ti_bandgap_write_counter(bgp, id, interval);
806 goto exit;
807 }
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300808
J Keerthy58bccd02013-04-01 12:04:42 -0400809 ret = ti_bandgap_write_counter_delay(bgp, id, interval);
810exit:
811 return ret;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300812}
813
814/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400815 * ti_bandgap_read_temperature() - report current temperature
Eduardo Valentin61603af2013-03-19 10:54:25 -0400816 * @bgp: pointer to bandgap instance
817 * @id: sensor id
818 * @temperature: resulting temperature
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300819 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400820 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300821 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400822int ti_bandgap_read_temperature(struct ti_bandgap *bgp, int id,
823 int *temperature)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300824{
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300825 u32 temp;
826 int ret;
827
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400828 ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300829 if (ret)
830 return ret;
831
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400832 spin_lock(&bgp->lock);
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400833 temp = ti_bandgap_read_temp(bgp, id);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400834 spin_unlock(&bgp->lock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300835
Pavel Macheke34238b2015-01-18 21:17:10 +0100836 ret = ti_bandgap_adc_to_mcelsius(bgp, temp, &temp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300837 if (ret)
838 return -EIO;
839
840 *temperature = temp;
841
842 return 0;
843}
844
845/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400846 * ti_bandgap_set_sensor_data() - helper function to store thermal
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300847 * framework related data.
Eduardo Valentin61603af2013-03-19 10:54:25 -0400848 * @bgp: pointer to bandgap instance
849 * @id: sensor id
850 * @data: thermal framework related data to be stored
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300851 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400852 * Return: 0 on success or the proper error code
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300853 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400854int ti_bandgap_set_sensor_data(struct ti_bandgap *bgp, int id, void *data)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300855{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400856 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300857 if (ret)
858 return ret;
859
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400860 bgp->regval[id].data = data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300861
862 return 0;
863}
864
865/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400866 * ti_bandgap_get_sensor_data() - helper function to get thermal
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300867 * framework related data.
Eduardo Valentin61603af2013-03-19 10:54:25 -0400868 * @bgp: pointer to bandgap instance
869 * @id: sensor id
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300870 *
Nishanth Menon169e8d02013-04-01 12:04:34 -0400871 * Return: data stored by set function with sensor id on success or NULL
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300872 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400873void *ti_bandgap_get_sensor_data(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300874{
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400875 int ret = ti_bandgap_validate(bgp, id);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300876 if (ret)
877 return ERR_PTR(ret);
878
Eduardo Valentin9879b2c2013-03-19 10:54:23 -0400879 return bgp->regval[id].data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300880}
881
Eduardo Valentine195aba2013-03-15 09:00:22 -0400882/*** Helper functions used during device initialization ***/
883
Eduardo Valentin31102a72013-03-15 09:00:26 -0400884/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400885 * ti_bandgap_force_single_read() - executes 1 single ADC conversion
886 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin31102a72013-03-15 09:00:26 -0400887 * @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 Menon169e8d02013-04-01 12:04:34 -0400891 *
892 * Return: 0
Eduardo Valentin31102a72013-03-15 09:00:26 -0400893 */
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300894static int
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400895ti_bandgap_force_single_read(struct ti_bandgap *bgp, int id)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300896{
Pavel Macheka4296d12015-01-18 21:20:51 +0100897 u32 counter = 1000;
898 struct temp_sensor_registers *tsr;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300899
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300900 /* Select single conversion mode */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400901 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400902 RMW_BITS(bgp, id, bgap_mode_ctrl, mode_ctrl_mask, 0);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300903
904 /* Start of Conversion = 1 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400905 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 1);
Eduardo Valentin194a54f2013-02-26 18:53:33 -0400906
Pavel Macheka4296d12015-01-18 21:20:51 +0100907 /* 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 Valentin194a54f2013-02-26 18:53:33 -0400915
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300916 /* Start of Conversion = 0 */
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400917 RMW_BITS(bgp, id, temp_sensor_ctrl, bgap_soc_mask, 0);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300918
Pavel Macheka4296d12015-01-18 21:20:51 +0100919 /* 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 Valentin8feaf0ce2012-07-12 19:02:29 +0300927 return 0;
928}
929
930/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400931 * ti_bandgap_set_continous_mode() - One time enabling of continuous mode
932 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300933 *
Eduardo Valentina84b6f42013-03-15 09:00:25 -0400934 * 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 Menon169e8d02013-04-01 12:04:34 -0400938 *
939 * Return: 0
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300940 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400941static int ti_bandgap_set_continuous_mode(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300942{
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300943 int i;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300944
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400945 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300946 /* Perform a single read just before enabling continuous */
Eduardo Valentin03e859d2013-03-19 10:54:21 -0400947 ti_bandgap_force_single_read(bgp, i);
Eduardo Valentind7f080e2013-03-19 10:54:18 -0400948 RMW_BITS(bgp, i, bgap_mode_ctrl, mode_ctrl_mask, 1);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +0300949 }
950
951 return 0;
952}
953
Eduardo Valentind3790b32013-03-15 09:00:30 -0400954/**
J Keerthy2f440b02013-04-01 12:04:45 -0400955 * 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 */
969int 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 Valentinba0049e2013-06-07 19:13:13 +0000985 spin_lock(&bgp->lock);
986
J Keerthy2f440b02013-04-01 12:04:45 -0400987 tsr = bgp->conf->sensors[id].registers;
988
989 /* Freeze and read the last 2 valid readings */
Eduardo Valentinba0049e2013-06-07 19:13:13 +0000990 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 1);
J Keerthy2f440b02013-04-01 12:04:45 -0400991 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 Valentinba0049e2013-06-07 19:13:13 +00001004 goto unfreeze;
J Keerthy2f440b02013-04-01 12:04:45 -04001005
1006 ret = ti_bandgap_adc_to_mcelsius(bgp, temp2, &t2);
1007 if (ret)
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001008 goto unfreeze;
J Keerthy2f440b02013-04-01 12:04:45 -04001009
1010 /* Fetch the update interval */
1011 ret = ti_bandgap_read_update_interval(bgp, id, &interval);
Ranganath Krishnane838ff82013-08-23 11:08:23 -05001012 if (ret)
Eduardo Valentinba0049e2013-06-07 19:13:13 +00001013 goto unfreeze;
J Keerthy2f440b02013-04-01 12:04:45 -04001014
Ranganath Krishnane838ff82013-08-23 11:08:23 -05001015 /* Set the interval to 1 ms if bandgap counter delay is not set */
1016 if (interval == 0)
1017 interval = 1;
1018
J Keerthy2f440b02013-04-01 12:04:45 -04001019 *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 Valentinba0049e2013-06-07 19:13:13 +00001024unfreeze:
1025 RMW_BITS(bgp, id, bgap_mask_ctrl, mask_freeze_mask, 0);
1026 spin_unlock(&bgp->lock);
J Keerthy2f440b02013-04-01 12:04:45 -04001027exit:
1028 return ret;
1029}
1030
1031/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001032 * ti_bandgap_tshut_init() - setup and initialize tshut handling
1033 * @bgp: pointer to struct ti_bandgap
Eduardo Valentind3790b32013-03-15 09:00:30 -04001034 * @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 Menon169e8d02013-04-01 12:04:34 -04001042 *
1043 * Return: 0 if no error, else error status
Eduardo Valentind3790b32013-03-15 09:00:30 -04001044 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001045static int ti_bandgap_tshut_init(struct ti_bandgap *bgp,
1046 struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001047{
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001048 int gpio_nr = bgp->tshut_gpio;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001049 int status;
1050
1051 /* Request for gpio_86 line */
1052 status = gpio_request(gpio_nr, "tshut");
1053 if (status < 0) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001054 dev_err(bgp->dev, "Could not request for TSHUT GPIO:%i\n", 86);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001055 return status;
1056 }
1057 status = gpio_direction_input(gpio_nr);
1058 if (status) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001059 dev_err(bgp->dev, "Cannot set input TSHUT GPIO %d\n", gpio_nr);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001060 return status;
1061 }
1062
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001063 status = request_irq(gpio_to_irq(gpio_nr), ti_bandgap_tshut_irq_handler,
1064 IRQF_TRIGGER_RISING, "tshut", NULL);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001065 if (status) {
1066 gpio_free(gpio_nr);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001067 dev_err(bgp->dev, "request irq failed for TSHUT");
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001068 }
1069
1070 return 0;
1071}
1072
Eduardo Valentin094b8ac2013-03-15 09:00:31 -04001073/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001074 * ti_bandgap_alert_init() - setup and initialize talert handling
1075 * @bgp: pointer to struct ti_bandgap
Eduardo Valentin094b8ac2013-03-15 09:00:31 -04001076 * @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 Menon169e8d02013-04-01 12:04:34 -04001083 *
1084 * Return: 0 if no error, else return corresponding error.
Eduardo Valentin094b8ac2013-03-15 09:00:31 -04001085 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001086static int ti_bandgap_talert_init(struct ti_bandgap *bgp,
1087 struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001088{
1089 int ret;
1090
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001091 bgp->irq = platform_get_irq(pdev, 0);
1092 if (bgp->irq < 0) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001093 dev_err(&pdev->dev, "get_irq failed\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001094 return bgp->irq;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001095 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001096 ret = request_threaded_irq(bgp->irq, NULL,
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001097 ti_bandgap_talert_irq_handler,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001098 IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001099 "talert", bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001100 if (ret) {
1101 dev_err(&pdev->dev, "Request threaded irq failed.\n");
1102 return ret;
1103 }
1104
1105 return 0;
1106}
1107
Eduardo Valentin61603af2013-03-19 10:54:25 -04001108static const struct of_device_id of_ti_bandgap_match[];
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -04001109/**
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001110 * ti_bandgap_build() - parse DT and setup a struct ti_bandgap
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -04001111 * @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 Valentin03e859d2013-03-19 10:54:21 -04001115 * will build a struct ti_bandgap out of the required DT entries.
Nishanth Menon169e8d02013-04-01 12:04:34 -04001116 *
1117 * Return: valid bandgap structure if successful, else returns ERR_PTR
1118 * return value must be verified with IS_ERR.
Eduardo Valentine9b6f8c2013-03-15 09:00:32 -04001119 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001120static struct ti_bandgap *ti_bandgap_build(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001121{
1122 struct device_node *node = pdev->dev.of_node;
1123 const struct of_device_id *of_id;
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001124 struct ti_bandgap *bgp;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001125 struct resource *res;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001126 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 Valentinf6843562013-03-19 10:54:24 -04001134 bgp = devm_kzalloc(&pdev->dev, sizeof(*bgp), GFP_KERNEL);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001135 if (!bgp) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001136 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
1137 return ERR_PTR(-ENOMEM);
1138 }
1139
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001140 of_id = of_match_device(of_ti_bandgap_match, &pdev->dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001141 if (of_id)
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001142 bgp->conf = of_id->data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001143
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001144 /* 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 Strandqvistfbe2ddc2014-06-02 23:25:30 +02001147 if (!bgp->regval) {
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001148 dev_err(&pdev->dev, "Unable to allocate mem for driver ref\n");
1149 return ERR_PTR(-ENOMEM);
1150 }
1151
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001152 i = 0;
1153 do {
1154 void __iomem *chunk;
1155
1156 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
1157 if (!res)
1158 break;
Thierry Reding97f4be602013-01-21 11:09:19 +01001159 chunk = devm_ioremap_resource(&pdev->dev, res);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001160 if (i == 0)
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001161 bgp->base = chunk;
Thierry Reding97f4be602013-01-21 11:09:19 +01001162 if (IS_ERR(chunk))
1163 return ERR_CAST(chunk);
Eduardo Valentin24796e12013-03-15 08:59:53 -04001164
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001165 i++;
1166 } while (res);
1167
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001168 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentin57d16172013-06-07 11:11:53 -04001169 bgp->tshut_gpio = of_get_gpio(node, 0);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001170 if (!gpio_is_valid(bgp->tshut_gpio)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001171 dev_err(&pdev->dev, "invalid gpio for tshut (%d)\n",
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001172 bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001173 return ERR_PTR(-EINVAL);
1174 }
1175 }
1176
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001177 return bgp;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001178}
1179
Eduardo Valentinf91ddfe2013-03-15 09:00:23 -04001180/*** Device driver call backs ***/
1181
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001182static
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001183int ti_bandgap_probe(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001184{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001185 struct ti_bandgap *bgp;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001186 int clk_rate, ret = 0, i;
1187
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001188 bgp = ti_bandgap_build(pdev);
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001189 if (IS_ERR(bgp)) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001190 dev_err(&pdev->dev, "failed to fetch platform data\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001191 return PTR_ERR(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001192 }
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001193 bgp->dev = &pdev->dev;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001194
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001195 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
1196 ret = ti_bandgap_tshut_init(bgp, pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001197 if (ret) {
1198 dev_err(&pdev->dev,
1199 "failed to initialize system tshut IRQ\n");
1200 return ret;
1201 }
1202 }
1203
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001204 bgp->fclock = clk_get(NULL, bgp->conf->fclock_name);
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001205 ret = IS_ERR(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001206 if (ret) {
1207 dev_err(&pdev->dev, "failed to request fclock reference\n");
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001208 ret = PTR_ERR(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001209 goto free_irqs;
1210 }
1211
Pavel Macheke34238b2015-01-18 21:17:10 +01001212 bgp->div_clk = clk_get(NULL, bgp->conf->div_ck_name);
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001213 ret = IS_ERR(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001214 if (ret) {
Pavel Macheke34238b2015-01-18 21:17:10 +01001215 dev_err(&pdev->dev, "failed to request div_ts_ck clock ref\n");
Eduardo Valentin0c12b5a2013-05-29 15:07:43 +00001216 ret = PTR_ERR(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001217 goto free_irqs;
1218 }
1219
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001220 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001221 struct temp_sensor_registers *tsr;
1222 u32 val;
1223
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001224 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001225 /*
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 Valentin03e859d2013-03-19 10:54:21 -04001230 val = ti_bandgap_readl(bgp, tsr->bgap_efuse);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001231 if (ret || !val)
1232 dev_info(&pdev->dev,
1233 "Non-trimmed BGAP, Temp not accurate\n");
1234 }
1235
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001236 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 Walmsleyc68789e2013-12-09 18:09:22 -08001239 clk_rate <= 0) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001240 ret = -ENODEV;
1241 dev_err(&pdev->dev, "wrong clock rate (%d)\n", clk_rate);
1242 goto put_clks;
1243 }
1244
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001245 ret = clk_set_rate(bgp->div_clk, clk_rate);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001246 if (ret)
1247 dev_err(&pdev->dev, "Cannot re-set clock rate. Continuing\n");
1248
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001249 bgp->clk_rate = clk_rate;
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001250 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001251 clk_prepare_enable(bgp->fclock);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -04001252
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001253
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001254 spin_lock_init(&bgp->lock);
1255 bgp->dev = &pdev->dev;
1256 platform_set_drvdata(pdev, bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001257
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001258 ti_bandgap_power(bgp, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001259
1260 /* Set default counter to 1 for now */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001261 if (TI_BANDGAP_HAS(bgp, COUNTER))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001262 for (i = 0; i < bgp->conf->sensor_count; i++)
1263 RMW_BITS(bgp, i, bgap_counter, counter_mask, 1);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001264
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001265 /* Set default thresholds for alert and shutdown */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001266 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001267 struct temp_sensor_data *ts_data;
1268
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001269 ts_data = bgp->conf->sensors[i].ts_data;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001270
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001271 if (TI_BANDGAP_HAS(bgp, TALERT)) {
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001272 /* Set initial Talert thresholds */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001273 RMW_BITS(bgp, i, bgap_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001274 threshold_tcold_mask, ts_data->t_cold);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001275 RMW_BITS(bgp, i, bgap_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001276 threshold_thot_mask, ts_data->t_hot);
1277 /* Enable the alert events */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001278 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_hot_mask, 1);
1279 RMW_BITS(bgp, i, bgap_mask_ctrl, mask_cold_mask, 1);
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001280 }
1281
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001282 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG)) {
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001283 /* Set initial Tshut thresholds */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001284 RMW_BITS(bgp, i, tshut_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001285 tshut_hot_mask, ts_data->tshut_hot);
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001286 RMW_BITS(bgp, i, tshut_threshold,
Eduardo Valentind3c291a2013-03-15 08:59:55 -04001287 tshut_cold_mask, ts_data->tshut_cold);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001288 }
1289 }
1290
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001291 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1292 ti_bandgap_set_continuous_mode(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001293
1294 /* Set .250 seconds time as default counter */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001295 if (TI_BANDGAP_HAS(bgp, COUNTER))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001296 for (i = 0; i < bgp->conf->sensor_count; i++)
1297 RMW_BITS(bgp, i, bgap_counter, counter_mask,
1298 bgp->clk_rate / 4);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001299
1300 /* Every thing is good? Then expose the sensors */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001301 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001302 char *domain;
1303
Eduardo Valentinf1553332013-04-08 08:19:13 -04001304 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 Valentin04a4d102012-09-11 19:06:55 +03001309
Eduardo Valentinf1553332013-04-08 08:19:13 -04001310 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 Valentin8feaf0ce2012-07-12 19:02:29 +03001316 }
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 Valentin03e859d2013-03-19 10:54:21 -04001323 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1324 ret = ti_bandgap_talert_init(bgp, pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001325 if (ret) {
1326 dev_err(&pdev->dev, "failed to initialize Talert IRQ\n");
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001327 i = bgp->conf->sensor_count;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001328 goto disable_clk;
1329 }
1330 }
1331
1332 return 0;
1333
Eduardo Valentinf1553332013-04-08 08:19:13 -04001334remove_last_cooling:
1335 if (bgp->conf->sensors[i].unregister_cooling)
1336 bgp->conf->sensors[i].unregister_cooling(bgp, i);
1337remove_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 Valentin8feaf0ce2012-07-12 19:02:29 +03001345disable_clk:
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001346 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001347 clk_disable_unprepare(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001348put_clks:
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001349 clk_put(bgp->fclock);
1350 clk_put(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001351free_irqs:
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001352 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001353 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1354 gpio_free(bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001355 }
1356
1357 return ret;
1358}
1359
1360static
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001361int ti_bandgap_remove(struct platform_device *pdev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001362{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001363 struct ti_bandgap *bgp = platform_get_drvdata(pdev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001364 int i;
1365
1366 /* First thing is to remove sensor interfaces */
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001367 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin262235b2013-04-08 08:19:14 -04001368 if (bgp->conf->sensors[i].unregister_cooling)
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001369 bgp->conf->sensors[i].unregister_cooling(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001370
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001371 if (bgp->conf->remove_sensor)
1372 bgp->conf->remove_sensor(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001373 }
1374
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001375 ti_bandgap_power(bgp, false);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001376
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001377 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001378 clk_disable_unprepare(bgp->fclock);
1379 clk_put(bgp->fclock);
1380 clk_put(bgp->div_clk);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001381
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001382 if (TI_BANDGAP_HAS(bgp, TALERT))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001383 free_irq(bgp->irq, bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001384
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001385 if (TI_BANDGAP_HAS(bgp, TSHUT)) {
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001386 free_irq(gpio_to_irq(bgp->tshut_gpio), NULL);
1387 gpio_free(bgp->tshut_gpio);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001388 }
1389
1390 return 0;
1391}
1392
Grygorii Strashko3992b622015-02-06 16:55:46 +02001393#ifdef CONFIG_PM_SLEEP
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001394static int ti_bandgap_save_ctxt(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001395{
1396 int i;
1397
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001398 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001399 struct temp_sensor_registers *tsr;
1400 struct temp_sensor_regval *rval;
1401
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001402 rval = &bgp->regval[i];
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001403 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001404
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001405 if (TI_BANDGAP_HAS(bgp, MODE_CONFIG))
1406 rval->bg_mode_ctrl = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001407 tsr->bgap_mode_ctrl);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001408 if (TI_BANDGAP_HAS(bgp, COUNTER))
1409 rval->bg_counter = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001410 tsr->bgap_counter);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001411 if (TI_BANDGAP_HAS(bgp, TALERT)) {
1412 rval->bg_threshold = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001413 tsr->bgap_threshold);
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001414 rval->bg_ctrl = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001415 tsr->bgap_mask_ctrl);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001416 }
1417
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001418 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1419 rval->tshut_threshold = ti_bandgap_readl(bgp,
J Keerthy76d2cd32012-09-11 19:06:52 +03001420 tsr->tshut_threshold);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001421 }
1422
1423 return 0;
1424}
1425
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001426static int ti_bandgap_restore_ctxt(struct ti_bandgap *bgp)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001427{
1428 int i;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001429
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001430 for (i = 0; i < bgp->conf->sensor_count; i++) {
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001431 struct temp_sensor_registers *tsr;
1432 struct temp_sensor_regval *rval;
1433 u32 val = 0;
1434
Eduardo Valentin9879b2c2013-03-19 10:54:23 -04001435 rval = &bgp->regval[i];
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001436 tsr = bgp->conf->sensors[i].registers;
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001437
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001438 if (TI_BANDGAP_HAS(bgp, COUNTER))
1439 val = ti_bandgap_readl(bgp, tsr->bgap_counter);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001440
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001441 if (TI_BANDGAP_HAS(bgp, TSHUT_CONFIG))
1442 ti_bandgap_writel(bgp, rval->tshut_threshold,
1443 tsr->tshut_threshold);
Radhesh Fadnisb87ea752012-11-13 14:10:04 -04001444 /* Force immediate temperature measurement and update
1445 * of the DTEMP field
1446 */
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001447 ti_bandgap_force_single_read(bgp, i);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001448
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001449 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 Valentin8feaf0ce2012-07-12 19:02:29 +03001460 }
1461 }
1462
1463 return 0;
1464}
1465
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001466static int ti_bandgap_suspend(struct device *dev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001467{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001468 struct ti_bandgap *bgp = dev_get_drvdata(dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001469 int err;
1470
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001471 err = ti_bandgap_save_ctxt(bgp);
1472 ti_bandgap_power(bgp, false);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -04001473
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001474 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001475 clk_disable_unprepare(bgp->fclock);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001476
1477 return err;
1478}
1479
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001480static int ti_bandgap_resume(struct device *dev)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001481{
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001482 struct ti_bandgap *bgp = dev_get_drvdata(dev);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001483
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001484 if (TI_BANDGAP_HAS(bgp, CLK_CTRL))
Eduardo Valentind7f080e2013-03-19 10:54:18 -04001485 clk_prepare_enable(bgp->fclock);
Radhesh Fadnis6c9c1d62013-02-26 18:53:25 -04001486
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001487 ti_bandgap_power(bgp, true);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001488
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001489 return ti_bandgap_restore_ctxt(bgp);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001490}
Jingoo Han5204f8c2014-02-27 20:43:02 +09001491static SIMPLE_DEV_PM_OPS(ti_bandgap_dev_pm_ops, ti_bandgap_suspend,
1492 ti_bandgap_resume);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001493
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001494#define DEV_PM_OPS (&ti_bandgap_dev_pm_ops)
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001495#else
1496#define DEV_PM_OPS NULL
1497#endif
1498
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001499static const struct of_device_id of_ti_bandgap_match[] = {
Eduardo Valentin1a312702012-07-12 19:02:31 +03001500#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 Valentin949f5a52012-07-12 19:02:32 +03001514#ifdef CONFIG_OMAP5_THERMAL
1515 {
1516 .compatible = "ti,omap5430-bandgap",
1517 .data = (void *)&omap5430_data,
1518 },
1519#endif
Eduardo Valentin25870e62013-05-29 15:07:45 +00001520#ifdef CONFIG_DRA752_THERMAL
1521 {
1522 .compatible = "ti,dra752-bandgap",
1523 .data = (void *)&dra752_data,
1524 },
1525#endif
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001526 /* Sentinel */
1527 { },
1528};
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001529MODULE_DEVICE_TABLE(of, of_ti_bandgap_match);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001530
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001531static struct platform_driver ti_bandgap_sensor_driver = {
1532 .probe = ti_bandgap_probe,
1533 .remove = ti_bandgap_remove,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001534 .driver = {
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001535 .name = "ti-soc-thermal",
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001536 .pm = DEV_PM_OPS,
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001537 .of_match_table = of_ti_bandgap_match,
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001538 },
1539};
1540
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001541module_platform_driver(ti_bandgap_sensor_driver);
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001542
1543MODULE_DESCRIPTION("OMAP4+ bandgap temperature sensor driver");
1544MODULE_LICENSE("GPL v2");
Eduardo Valentin03e859d2013-03-19 10:54:21 -04001545MODULE_ALIAS("platform:ti-soc-thermal");
Eduardo Valentin8feaf0ce2012-07-12 19:02:29 +03001546MODULE_AUTHOR("Texas Instrument Inc.");