aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/intel_sst/intelmid_adc_control.h
blob: 65d5c3988762cf390bcbd3494e53fb958ed7d4af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef __INTELMID_ADC_CONTROL_H__
#define __INTELMID_ADC_CONTROL_H_
/*
 *  intelmid_adc_control.h - Intel SST Driver for audio engine
 *
 *  Copyright (C) 2008-10 Intel Corporation
 *  Authors:	R Durgadadoss <r.durgadoss@intel.com>
 *		Dharageswari R <dharageswari.r@intel.com>
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *  Common private ADC declarations for SST
 */


#define MSIC_ADC1CNTL1		0x1C0
#define MSIC_ADC_ENBL		0x10
#define MSIC_ADC_START		0x08

#define MSIC_ADC1CNTL3		0x1C2
#define MSIC_ADCTHERM_ENBL	0x04
#define MSIC_ADCRRDATA_ENBL	0x05

#define MSIC_STOPBIT_MASK	16
#define MSIC_ADCTHERM_MASK	4

#define ADC_CHANLS_MAX		15 /* Number of ADC channels */
#define ADC_LOOP_MAX		(ADC_CHANLS_MAX - 1)

/* ADC channel code values */
#define AUDIO_DETECT_CODE	0x06

/* ADC base addresses */
#define ADC_CHNL_START_ADDR	0x1C5	/* increments by 1 */
#define ADC_DATA_START_ADDR     0x1D4   /* increments by 2 */


/**
 * configure_adc - enables/disables the ADC for conversion
 * @val: zero: disables the ADC non-zero:enables the ADC
 *
 * Enable/Disable the ADC depending on the argument
 *
 * Can sleep
 */
static inline int configure_adc(int val)
{
	int ret;
	struct sc_reg_access sc_access = {0,};


	sc_access.reg_addr = MSIC_ADC1CNTL1;
	ret = sst_sc_reg_access(&sc_access, PMIC_READ, 1);
	if (ret)
		return ret;

	if (val)
		/* Enable and start the ADC */
		sc_access.value |= (MSIC_ADC_ENBL | MSIC_ADC_START);
	else
		/* Just stop the ADC */
		sc_access.value &= (~MSIC_ADC_START);
	sc_access.reg_addr = MSIC_ADC1CNTL1;
	return sst_sc_reg_access(&sc_access, PMIC_WRITE, 1);
}

/**
 * reset_stopbit - sets the stop bit to 0 on the given channel
 * @addr: address of the channel
 *
 * Can sleep
 */
static inline int reset_stopbit(uint16_t addr)
{
	int ret;
	struct sc_reg_access sc_access = {0,};
	sc_access.reg_addr = addr;
	ret = sst_sc_reg_access(&sc_access, PMIC_READ, 1);
	if (ret)
		return ret;
	/* Set the stop bit to zero */
	sc_access.reg_addr = addr;
	sc_access.value = (sc_access.value) & 0xEF;
	return sst_sc_reg_access(&sc_access, PMIC_WRITE, 1);
}

/**
 * find_free_channel - finds an empty channel for conversion
 *
 * If the ADC is not enabled then start using 0th channel
 * itself. Otherwise find an empty channel by looking for a
 * channel in which the stopbit is set to 1. returns the index
 * of the first free channel if succeeds or an error code.
 *
 * Context: can sleep
 *
 */
static inline int find_free_channel(void)
{
	int ret;
	int i;

	struct sc_reg_access sc_access = {0,};

	/* check whether ADC is enabled */
	sc_access.reg_addr = MSIC_ADC1CNTL1;
	ret = sst_sc_reg_access(&sc_access, PMIC_READ, 1);
	if (ret)
		return ret;

	if ((sc_access.value & MSIC_ADC_ENBL) == 0)
		return 0;

	/* ADC is already enabled; Looking for an empty channel */
	for (i = 0; i < ADC_CHANLS_MAX; i++) {

		sc_access.reg_addr = ADC_CHNL_START_ADDR + i;
		ret = sst_sc_reg_access(&sc_access, PMIC_READ, 1);
		if (ret)
			return ret;

		if (sc_access.value & MSIC_STOPBIT_MASK) {
			ret = i;
			break;
		}
	}
	return (ret > ADC_LOOP_MAX) ? (-EINVAL) : ret;
}

/**
 * mid_initialize_adc - initializing the ADC
 * @dev: our device structure
 *
 * Initialize the ADC for reading thermistor values. Can sleep.
 */
static inline int mid_initialize_adc(void)
{
	int base_addr, chnl_addr;
	int ret;
	static int channel_index;
	struct sc_reg_access sc_access = {0,};

	/* Index of the first channel in which the stop bit is set */
	channel_index = find_free_channel();
	if (channel_index < 0) {
		pr_err("No free ADC channels");
		return channel_index;
	}

	base_addr = ADC_CHNL_START_ADDR + channel_index;

	if (!(channel_index == 0 || channel_index == ADC_LOOP_MAX)) {
		/* Reset stop bit for channels other than 0 and 12 */
		ret = reset_stopbit(base_addr);
		if (ret)
			return ret;

		/* Index of the first free channel */
		base_addr++;
		channel_index++;
	}

	/* Since this is the last channel, set the stop bit
	   to 1 by ORing the DIE_SENSOR_CODE with 0x10 */
	sc_access.reg_addr = base_addr;
	sc_access.value = AUDIO_DETECT_CODE | 0x10;
	ret = sst_sc_reg_access(&sc_access, PMIC_WRITE, 1);
	if (ret) {
		pr_err("unable to enable ADC");
		return ret;
	}

	chnl_addr = ADC_DATA_START_ADDR + 2 * channel_index;
	pr_debug("mid_initialize : %x", chnl_addr);
	configure_adc(1);
	return chnl_addr;
}
#endif