1 /* $OpenBSD: pt2254a.c,v 1.1 2002/04/25 04:56:59 mickey Exp $ */
2 /*
3 * Copyright (c) 2002 Vladimir Popov <jumbo@narod.ru>.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25 /*
26 * Princeton Technology Corp.'s Electronic Volume Controller IC PT2254A
27 * http://www.princeton.com.tw
28 *
29 * PT2254A is an electronic volume controller IC utilizing CMOS Technology
30 * specially designed for use in audio equipment. It has two built-in
31 * channels making it highly suitable for mono and stereo sound applications.
32 * Through the specially designated signals that are applied externally to
33 * the data, clock and strobe input pins, PT2254A can control attenuation
34 * and channel balance.
35 */
36
37 #include <sys/param.h>
38
39 #include <dev/ic/pt2254a.h>
40
41 u_int32_t
42 pt2254a_encode_volume(u_int8_t *current, u_int8_t max) {
43 u_int32_t ret = 0ul;
44 u_int8_t vol, tens, ones;
45 int steps;
46
47 /*
48 * Volume management is done nonlinear.
49 *
50 * Approximate the curve with three lines:
51 * low volume: y = - (48 /(max/3)) * x + 68
52 * middle volume: y = - (14 /(max/3)) * x + 34
53 * high volume: y = - (6 /(max/3)) * x + 18
54 */
55 if (*current > 0 && *current <= max / 3) {
56 vol = PT2254A_MAX_ATTENUATION - (144 * (int)(*current) / max);
57 } else if (*current > max / 3 && *current <= 2 * max / 3) {
58 vol = 34 - (42 * (int)(*current) / max);
59 } else if (*current > 2 * max / 3) {
60 vol = 18 - (18 * (int)(*current) / max);
61 } else vol = PT2254A_MAX_ATTENUATION;
62
63 /* Report adjusted volume */
64 /* *current = max - (vol * max / PT2254A_MAX_ATTENUATION); */
65 steps = *current * PT2254A_MAX_ATTENUATION / max;
66 *current = steps * max / PT2254A_MAX_ATTENUATION;
67
68 tens = vol / 10;
69 ones = vol - tens * 10;
70
71 ret = PT2254A_ATTENUATION_MAJOR(tens) | PT2254A_ATTENUATION_MINOR(ones);
72
73 return ret;
74 }
75
76 u_int32_t
77 pt2254a_compose_register(u_int32_t lvol, u_int32_t rvol, int left, int right) {
78 u_int32_t ret = 0ul;
79
80 if (left == USE_CHANNEL) {
81 ret |= PT2254A_LEFT_CHANNEL;
82 ret |= lvol;
83 }
84 if (right == USE_CHANNEL) {
85 ret |= PT2254A_RIGHT_CHANNEL;
86 ret |= rvol;
87 }
88
89 ret |= PT2254A_ZERO_PADDING;
90 ret |= PT2254A_EMPTY_BIT;
91
92 return ret;
93 }